mybatis基础
mybatis简介
mybatis是apache的一个开源项目ibatis,
mybatis支持定制化SQL、存储过程以及高级映射,mybatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集
mybatis可以使用简单的xml或注解来配置和映射原生信息,将接口和Java的pojos(Plain old java objects,普通的java对象)映射成数据库的记录
添加依赖:在maven项目下添加mybatis依赖
pom.xml
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
创建mybatis.xml配置文件,该配置文件包含一个configuration节点,里面有配置信息,分别是环境和映射,其中环境有datasource,里面有连接数据库的四个字符串,映射地址是绝对地址
mybatis.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hello"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/qfedu/pojo/UserMapper.xml"/>
</mappers>
</configuration>
映射文件,每个mapper文件都有一个自己的namespace,每个方法对应自己的sql语句,每个sql语句对应有一个id,整个项目中所有的namespace、id必须是唯一的,根据不同的功能,使用不同的标签,
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="com.qfedu.pojo.UserMapper">
<select id="selectUser" resultType="com.qfedu.pojo.User">
select * from user
</select>
<select id="selectUserCount" resultType="int">
select count(1) from user;
</select>
<select id="selectUsersByPage1" resultType="com.qfedu.pojo.User">
select * from user limit 3
</select>
<select id="selectUsersByPage2" resultType="com.qfedu.pojo.User">
select * from user limit #{pageSize}
</select>
<select id="selectUsersByPage3" resultType="com.qfedu.pojo.User">
select * from user limit #{startIndex}, #{pageSize}
</select>
<insert id="saveUser">
insert into user values(null, #{username}, #{password}, #{age}, #{addr});
</insert>
</mapper>
测试,
- 使用mybatis的配置文件以及SqlSessionFactoryBuilder建造者模式创建SqlSessoinFactory对象
- 使用SqlSessionFactory对象的openSession()方法来得到一个sqlSession对象,用该对象即可完成对下给你的所有crud的操作
- 使用sqlSession对象来完成crud操作
- 关闭资源
private SqlSessionFactory sf = null;
private SqlSession session = null;
@Before
public void setUp(){
try {
sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
session = sf.openSession(true);
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void tearDown(){
if(session != null){
session.close();
session = null;
}
}
@Test
public void testGetAllUsers(){
List<User> users = session.selectList("com.qfedu.pojo.UserMapper.selectUser");
for (User u : users) {
System.out.println(u);
}
}
@Test
public void testGetAllUsersCount(){
Integer count = session.selectOne("com.qfedu.pojo.UserMapper.selectUserCount");
System.out.println(count);
}
@Test
public void testGetUsersByPage3(){
Map<String, Integer> map = new HashMap<>();
map.put("startIndex", 10);
map.put("pageSize", 5);
List<User> users = session.selectList("com.qfedu.pojo.UserMapper.selectUsersByPage3", map);
for (User u : users) {
System.out.println(u);
}
}
@Test
public void testSaveUser(){
User u = new User();
u.setUsername("lisi");
u.setPassword("888888");
u.setAddr("wuhan");
u.setAge(20);
int result = session.insert("com.qfedu.pojo.UserMapper.saveUser", u);
System.out.println(result);
}
}