MyBatis(三)代理Dao方式的CRUD操作
目录
一、代理Dao方式的CRUD操作
test
mapper
sql
一、代理Dao方式的CRUD操作
在入门案例中有很多重复的地方进行提取
@Before
public void init() throws Exception {
// 加载配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
// 创建工厂对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 创建Session对象
session = factory.openSession();
// 获取到代理对象
mapper = session.getMapper(UserMapper.class);
}
@After
public void destory() throws IOException {
in.close();
session.close();
}
把befor和after提取出来后,再进行测试就简便了许多
test
/**
* 测试查询所有的方法
* @throws Exception
*/
@Test
public void testFindAll() throws Exception {
List<User> list = mapper.findAll();
// 遍历
for (User user : list) {
System.out.println(user);
}
in.close();
}
@Test
public void testFindById() throws Exception {
User user = mapper.findById(41);
System.out.println(user);
in.close();
}
@Test
public void testInsert() throws Exception {
User user = new User();
user.setUsername("美美");
user.setBirthday(new Date());
user.setSex("男");
user.setAddress("顺义");
mapper.insert(user);
session.commit();
System.out.println(user.getId());
}
@Test
public void testUpdate() throws Exception {
User user = mapper.findById(41);
user.setUsername("小凤");
mapper.update(user);
session.commit();
}
@Test
public void testDelete() throws Exception {
mapper.delete(48);
session.commit();
}
// 第一种
@Test
public void testFindByName() throws Exception {
List<User> list = mapper.findByName("%王%");
for (User user : list) {
System.out.println(user);
}
}
// 第二种
@Test
public void testFindByName() throws Exception {
List<User> list = mapper.findByName("王");
for (User user : list) {
System.out.println(user);
}
}
@Test
public void testFindByCount() throws Exception {
Integer count = mapper.findByCount();
System.out.println("总记录数:"+count);
}
mapper
public interface UserMapper {
public List<User> findAll();
public User findById(Integer userId);
public void insert(User user);
public void update(User user);
public void delete(Integer userId);
public List<User> findByName(String username);
public Integer findByCount();
}
sql
<?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.qcbyjy.mapper.UserMapper">
<select id="findAll" resultType="com.qcbyjy.domain.User">
select * from user
</select>
<!--
通过id查询
SQL语句使用#{占位符的名称,名称可以任意},仅限于基本数据类型和String类型
-->
<select id="findById" resultType="com.qcbyjy.domain.User" parameterType="int">
select * from user where id = #{id};
</select>
<!--保存操作-->
<insert id="insert" parameterType="com.qcbyjy.domain.User">
/*
keyProperty表示要返回的属性名称
order取值AFTER表示插入数据后的行为
resultType表示返回值的类型
*/
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select last_insert_id();
</selectKey>
insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})
</insert>
<!-- 修改 -->
<update id="update" parameterType="com.qcbyjy.domain.User">
update user set username = #{username},birthday = #{birthday},sex = #{sex},address=#{address} where id = #{id}
</update>
<!-- 删除 -->
<delete id="delete" parameterType="Integer">
delete from user where id = #{id}
</delete>
<!-- 模糊查询 -->
<select id="findByName" resultType="com.qcbyjy.domain.User" parameterType="string">
<!-- 第一种方式的SQL语句
select * from user where username like #{username}
-->
<!-- 第二章SQL语句的编写 强调:'%${value}%'不能修改,固定写法(不推荐使用) -->
select * from user where username like '%${value}%'
</select>
<!-- 具体函数的查询 -->
<select id="findByCount" resultType="int">
select count(*) from user
</select>
</mapper>