Mybatis
Posted 大道至简
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis相关的知识,希望对你有一定的参考价值。
Mybatis
1、mybatis介绍
mybatis是sqlmap技术,对jdbc进行封装,将大量的sql语句外部化。
2、体验mybatis
准备数据库和表
mysql>create table users(id int primary key auto_increment , name varchar(20) , age int) ;
创建模块引入maven依赖
[pom.xml]
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.oldboy</groupId> <artifactId>mybatis</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.17</version> </dependency> <!--junit单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <!-- mybatis类库 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.1</version> </dependency> </dependencies> </project>
创建javabean
/** * User类 */ public class User { private Integer id; private String name ; private int age ; //get//set }
创建映射文件
[resources/UserMapper.xml]
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 定义名字空间 --> <mapper namespace="users"> <!-- 定义insert语句 ,获得生成的id字段--> <insert id="insert" > insert into users(name,age) values(#{name},#{age}) </insert> </mapper>
创建配置文件
[resources/mybatis-config.xml]
<?xml version = "1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/big12"/> <property name="username" value="root"/> <property name="password" value="root"/> </properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="UserMapper.xml"/> </mappers> </configuration>
编写测试类,实现插入
@Test
public void testInsert() throws IOException {
//加载配置文件
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//创建会话工厂(builder模式)
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);
//开启会话,相当于连接
SqlSession sess = sf.openSession();
User u = new User() ;
u.setName("tom");
u.setAge(12);
//执行插入操作
sess.insert("users.insert" , u) ;
sess.commit();
sess.close();
}
3、实现mybatis的基本操作
3.1 update
在映射文件中增加update语句
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 定义名字空间 --> <mapper namespace="users"> <update id="update"> update users set name = #{name} , age = #{age} where id = #{id} </update> </mapper>
调用session.update方法
... User u = new User() ; u.setName("tomas"); u.setAge(22); u.setId(1); sess.update("users.update" , u) ;
3.2 select
3.2.1 查询一个
映射文件增加查询语句
[userMapper.xml]
... <select id="selectById" resultType="_User"> select * from users where id = #{id} </select> ...
配置文件添加别名
[mybatis-config.xml]
<?xml version = "1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> ... <typeAliases> <typeAlias type="com.oldboy.mybatis.domain.User" alias="_User"/> </typeAliases> ... </configuration>
?
API调用
User u = sess.selectOne("users.selectById" , 1) ;
3.2.2 查询多个
添加sql语句
[userMapper.xml]
... <select id="selectAll" resultType="_User"> select * from users </select> ...
API调用
List<User> list = sess.selectList("users.selectAll" ) ;
3.3 delete
添加sql语句
[UserMapper.xml]
... <delete id="delete"> delete from users where id = #{id} </delete> ...
API调用
sess.delete("users.delete" , 1) ;
4、获取数据库主键值
数据表如果主键是自动增长的话,如果要在程序插入时得到主键值可修改映射文件,具体如下:
[userMapper.xml]
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
...
</insert>
以上是关于Mybatis的主要内容,如果未能解决你的问题,请参考以下文章
SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper
MYBATIS05_ifwherechoosewhentrimsetforEach标签sql片段