CRUD
1.namespace
namespace中的包名要和Dao/Mapper接口的包名一致
2.select
选择,查询语句;
- id: 对应的namespace中的方法名
- resultType: SQL语句执行的返回值
- parameterType: 参数类型
-
编写接口
User getUserById(int id);//根据ID查询用户
-
编写对应mapper中的sql语句
<select id="getUserById" parameterType="int" resultType="cn.pinked.pojo.User"> select * from mybatis.user where id = #{id} </select>
-
测试
@Test public void getUserById(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(2); System.out.println(user); sqlSession.close(); }
3.insert
int addUser(User user);//插入一个用户
<insert id="addUser" parameterType="cn.pinked.pojo.User">
insert into mybatis.user(id, name, pwd) values(#{id},#{name},#{pwd});
</insert>
-
@Test public void addUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int res = mapper.addUser(new User(4, "李四", "123456")); sqlSession.commit();//提交事务 sqlSession.close(); }
4.update
int updateUser(User user);//修改用户
<update id="updateUser" parameterType="cn.pinked.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};
</update>
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.updateUser(new User(4,"李大四","1234"));
sqlSession.commit();//提交事务
sqlSession.close();
}
5.delete
int deleteUser(int id);//删除一个用户
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id};
</delete>
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.deleteUser(4);
sqlSession.commit();//提交事务
sqlSession.close();
}
6.模糊查询
-
List<User> getUserLike(String value);//模糊查询
-
<select id="getUserLike" resultType="cn.pinked.pojo.User"> select * from mybatis.user where name like #{value} </select>
-
@Test public void getUserLike(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserLike("%张%"); for (User user : userList) { System.out.println(user); } sqlSession.close(); }
7.map的使用
如果在实体类中,或数据库中的表,字段或者参数过多,可以使用map传递数据
-
int addUser2(Map<String, Object> map);//使用map
-
<insert id="addUser2" parameterType="map"> insert into mybatis.user(id, name, pwd) values(#{userid},#{username},#{password}); </insert>
-
@Test public void addUser2(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("userid",4); map.put("username","李四"); map.put("password","123456"); int res = mapper.addUser2(map); sqlSession.commit();//提交事务 sqlSession.close(); }
map传递参数,直接在sql中取出key即可 ---------------------- parameterType="map"
对象传递参数,直接在sql中取出对象的属性即可-------------parameterType="Object"
只有一个基本类型参数的情况下,可以直接在sql中取到
多个参数用map或者注解