1、namespace
-
将上面案例中的UserMapper接口改名为 UserDao;
-
将UserMapper.xml中的namespace改为为UserDao的路径 .
-
再次测试
结论:
配置文件中namespace中的名称为对应Mapper接口或者Dao接口的完整包名,必须一致!
2、select
选择查询语句;
-
id:就是对应的namespace中的方法名
-
resultType:Sql语句执行的返回值
-
parameterType:参数类型
-
编写接口
//获取全部用户 List<User> getUserList(); //根据ID查询用户名 User getUserById(int id);
-
编写对应的Mapper中的SQL语句
<!--select查询语句--> <select id="getUserList" resultType="com.li.pojo.User"> select * from mybatis.user </select> <select id="getUserById" parameterType="int" resultType="com.li.pojo.User"> select * from mybatis.user where id=#{id} </select>
-
测试
@Test public void getUser(){ //第一步,获取sqlsession对象 SqlSession sqlSession = MybatisUtils.getsqlSession(); //执行sql //方式一:getMapper UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getUserList(); //方式二 // List<User> userList = sqlSession.selectList("com.li.dao.UserDao.getUserList"); for (User user : userList) { System.out.println(user); } sqlSession.close(); } @Test public void getUserById(){ //第一步,获取sqlsession对象 SqlSession sqlSession = MybatisUtils.getsqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User userById = mapper.getUserById(1); System.out.println(userById); sqlSession.close(); }
3、Insert
-
编写接口
//插入用户 int addUser(User user);
-
编写对应的Mapper中的SQL语句
<!-- 对象中的属性可以直接取出来--> <insert id="addUser" parameterType="com.li.pojo.User"> insert into mybatis.user (id,name,pwd) value (#{id},#{name},#{pwd}) </insert>
-
测试
//增删改需要加事务 @Test public void add(){ SqlSession sqlsession = MybatisUtils.getsqlSession(); UserMapper mapper = sqlsession.getMapper(UserMapper.class); int i = mapper.addUser(new User(4, "李乐园", "123456")); if(i>0){ System.out.println("成功"); } //提交事务 sqlsession.commit(); sqlsession.close(); }
4、update
-
编写接口
//修改用户 int updateUser(User user);
-
编写对应的Mapper中的SQL语句
<update id="updateUser" parameterType="com.li.pojo.User"> update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}; </update>
-
测试
@Test public void update(){ SqlSession sqlSession = MybatisUtils.getsqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updateUser(new User(4, "lly", "111111")); sqlSession.commit(); sqlSession.close(); }
5、delete
-
编写接口
//删除用户 int deleteUser(int id);
-
编写对应的Mapper中的SQL语句
<delete id="deleteUser" parameterType="int"> delete from mybatis.user where id=#{id}; </delete>
-
测试 @Test public void delete(){ SqlSession sqlSession = MybatisUtils.getsqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.deleteUser(4); sqlSession.commit(); sqlSession.close(); }
6、万能的Map
假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用map
//万能的map
int addUser2(Map<String,Object> map);
<insert id="addUser2" parameterType="map">
insert into mybatis.user (id,pwd) value (#{userid},#{password})
</insert>
@Test
public void add2(){
SqlSession sqlSession = MybatisUtils.getsqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userid", 6);
map.put("password", "123456");
mapper.addUser2(map);
sqlSession.commit();
sqlSession.close();
}
Map传递参数,直接在sql中取出key即可
对象传递参数,直接在SQL中取对象的属性即可
只有一个基本类型参数的情况下,可以在SQL中直接取到
多个参数用Map或者注解
7、模糊查询
模糊查询like语句该怎么写?
第1种:在Java代码中添加sql通配符。
string wildcardname = “%smi%”;
list<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like #{value}
</select>
第2种:在sql语句中拼接通配符,会引起sql注入
string wildcardname = “smi”;
list<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like "%"#{value}"%"
</select>