Select查询
需求:根据id查询用户
在UserMapper接口类中添加方法:
public interface UserMapper {
//根据id查询用户
User getUserById(int id);
}
在UserMapper.xml配置文件添加Select语句
注意id要和方法名一致
<select id="getUserById" resultType="com.kuang.pojo.User" parameterType="int">
select * from user where id = #{id}
</select>
测试:
@Test
public void getUserById(){
//1、获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
//关闭sqlSession
sqlSession.close();
}
insert添加
需求:给数据库增加一个用户
1、在UserMapper接口中添加对应的方法
//insert一个用户
int addUser(User user);
2、在UserMapper.xml中添加insert语句
<!--对象中的属性,可以直接取出来-->
<insert id="addUser" parameterType="com.kuang.pojo.User">
insert into user(id,name,pwd) values(#{id},#{name},#{pwd})
</insert>
3、测试
//增删改需要提交事务
@Test
public void addUser(){
//1、获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.addUser(new User(4, "哈哈", "123333"));
sqlSession.commit();
//关闭sqlSession
sqlSession.close();
}
Update修改
1、在UserMapper接口中添加对应的方法
//修改用户
int updateUser(User user);
2、在UserMapper.xml中添加insert语句
<update id="updateUser" parameterType="com.kuang.pojo.User">
update user set name = #{name},pwd=#{pwd} where id = #{id}
</update>
3、测试
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"呵呵","123123"));
sqlSession.commit();
sqlSession.close();
}
delete删
需求:根据id删除一个用户
1、在UserMapper接口中添加对应的方法
//删除一个用户
int deleteUser(int id);
2、在UserMapper.xml中添加insert语句
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id};
</delete>
3、测试
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
小结:
所有的增删改操作都需要提交事务!
接口所有的普通参数,尽量都写上@Param参数,尤其是多个参数时,必须写上! 有时候根据业务的需求,可以考虑使用map传递参数!
为了规范操作,在SQL的配置文件中,我们尽量将Parameter参数和resultType都写上!
模糊查询like语句写法:
1、在UserMapper接口中添加对应的方法
List<User> getUserLike(String value);
2、方式一:在Java代码中添加sql通配符。
<select id="getUserLike" resultType="com.kuang.pojo.User">
select * from user where name like #{value}
</select>
3、测试-方式一
@Test
public void getUserLike(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//List<User> userLike = mapper.getUserLike("李");
List<User> userLike = mapper.getUserLike("%李%");
for (User user : userLike) {
System.out.println(user);
}
sqlSession.close();
}
4、方式二:在sql语句中拼接通配符,会引起sql注入。
<select id="getUserLike" resultType="com.kuang.pojo.User">
select * from user where name like "%"#{value}"%"
</select>
3、测试-方式二
@Test
public void getUserLike(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userLike = mapper.getUserLike("李");
//List<User> userLike = mapper.getUserLike("%李%");
for (User user : userLike) {
System.out.println(user);
}
sqlSession.close();
}