2、mybatis增删改查
select
select标签是mybatis最常用的标签之一;select语句有很多属性,用于配置每一条查询语句,包括返回值类型(完整的类名或者别名),参数(po类或者map),命名空间的唯一标识
UserDao.java:
List<User> selectUser();
User selectOne(int id);
User selectMap(Map<String,String> map);
UserDao.xml:
<select id="selectUser" resultType="com.test.po.User">
select * from user
</select>
<select id="selectOne" resultType="com.test.po.User" parameterType="int">
select * from user where id = ${id}
</select>
<select id="selectMap" resultType="com.test.po.User">
select * from user where id = #{id}
</select>
测试类:
@Test
public void selectUser(){
SqlSession sqlSession = MybatisUtils.getSession();
List<User> users = sqlSession.selectList("com.test.dao.UserDao.selectUser");
System.out.println(users);
}
@Test
public void selectOne(){
SqlSession sqlSession = MybatisUtils.getSession();
User user = sqlSession.selectOne("com.test.dao.UserDao.selectOne",2);
System.out.println(user);
}
@Test
public void selectMap(){
SqlSession sqlSession = MybatisUtils.getSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
//适用于参数较多时,建议使用map传参
Map<String,String> map = new HashMap<>();
map.put("id","2");
User user = userDao.selectMap(map);
System.out.println(user);
}
insert
一般使用insert标签执行插入操作,配置与select标签类似
UserDao.java:
int saveUser(User user);
UserDao.xml:
<insert id="saveUser" parameterType="com.test.po.User">
insert into user values(${id},#{name},#{pwd})
</insert>
测试类:
@Test
public void saveUser(){
SqlSession sqlSession = MybatisUtils.getSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = new User();
user.setId(4);
user.setName("xxx");
user.setPwd("xxxx1");
int ch = userDao.saveUser(user);
sqlSession.commit();
System.out.println(ch);
}
注:增、删、改操作需要提交事务
update
一般使用update标签进行更新操作,配置与select类似
UserDao.java:
int updateUser(User user);
UserDao.xml:
<update id="updateUser" parameterType="com.test.po.User">
update user set name = #{name},pwd = #{pwd} where id = #{id}
</update>
测试类:
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = new User();
user.setPwd("xxyy");
user.setName("xy");
user.setId(4);
int ch = userDao.updateUser(user);
sqlSession.commit();
System.out.println(ch);
}
delete
一般使用delete进行删除操作,配置与select类似
UserDao.java:
int deleteUser(int id);
UserDao.xml:
<delete id="deleteUser" parameterType="int">
delete from user where id = ${id}
</delete>
测试类:
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
int ch = userDao.deleteUser(4);
sqlSession.commit();
System.out.println(ch);
}
小结
1、所有的增删改操作都需要提交
2、接口的所有普通参数,尽量都写@Param注解,尤其是多个参数时
3、根据业务需求,可以考虑使用map
4、为了规范操作,在sql配置文件中,尽量将parameter参数和resultType都写上
模糊查询
方式1:
UserDao.java:
User selectWithPart(String param);
UserDao.xml:
<select id="selectWithPart" parameterType="java.lang.String" resultType="com.test.po.User">
select * from user where name like "%"#{param}"%"
</select>
测试类:
@Test
public void selectWithPart(){
SqlSession sqlSession = MybatisUtils.getSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = userDao.selectWithPart("三");
System.out.println(user);
}
方式2:
UserDao.java:
User selectWithPart2(String param);
UserDao.xml:
<select id="selectWithPart2" parameterType="java.lang.String" resultType="com.test.po.User">
select * from user where name like #{param}
</select>
测试类:
@Test
public void selectWithPart2(){
SqlSession sqlSession = MybatisUtils.getSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = userDao.selectWithPart2("%三%");
System.out.println(user);
}