1、namespace
namespace中的包名要和 Dao/mapper 接口的包名一致!
2、select
选择,查询语句;
- id : 就是对应的namespace中的方法名;
- resultType:Sql语句执行的返回值!
- parameterType : 参数类型!
-
编写接口
//根据ID查询用户 User getUserById(int id);
-
编写对应的mapper中的sql语句
<select id="getUserById" parameterType="int" resultType="com.kuang.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(1); System.out.println(user); sqlSession.close(); }
3、Insert
<!--对象中的属性,可以直接取出来-->
<insert id="addUser" parameterType="com.kuang.pojo.User">
insert into mybatis.user (id, name, pwd) values (#{id},#{name},#{pwd});
</insert>
4、update
<update id="updateUser" parameterType="com.kuang.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id = #{id} ;
</update>
5、Delete
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id};
</delete>
注意点:
- 增删改需要提交事务!
6、分析错误
- 标签不要匹配错
- resource 绑定mapper,需要使用路径!
<mapper resource="com/loading/dao/UserMapper.xml"/>
- 程序配置文件必须符合规范!
- NullPointerException,没有注册到资源!
- 输出的xml文件中存在中文乱码问题!
- maven资源没有导出问题!
- 当添加用户和更新用户时,返回值是User对象在调用其方法传参时,要new 出对象
CRUD的主要代码:
- Mapper接口的方法
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据ID查询用户
User getUserById(int id);
//根据ID删除用户
int deleteUserById(int id);
//增加用户
int insertUser(User user);
//更新用户
int updateUser(User user);
}
- 对应的Mapper.xml注册信息和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">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.loading.dao.UserMapper">
<select id="getUserList" resultType="com.loading.pojo.User">
select * from mybatis.user;
</select>
<select id="getUserById" resultType="com.loading.pojo.User" parameterType="int">
select * from mybatis.user where id = #{id};
</select>
<delete id="deleteUserById" parameterType="int" >
/* delete from mybatis.user where id =1;*/
delete from mybatis.user where id = #{id};
</delete>
<insert id="insertUser" parameterType="com.loading.pojo.User">
/* insert into mybatis.user (id,name,pwd) values (1,"loading","12345");*/
insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="com.loading.pojo.User" >
/* update mybatis.user set name = "胡歌", pwd = "12345" where id= 1; */
update mybatis.user set name =#{name}, pwd =#{pwd} where id=#{id};
</update>
</mapper>
- 在测试类中进行编写
@Test
//增加用户
public void insertUser(){
//获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.insertUser(new User(2, "直直", "12345"));
if(i>0){
System.out.println("插入用户成功");
}
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
}finally {
sqlSession.close();
}
}
7、万能Map
假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用Map!
//万能的Map
//使用map增加用户
int insertUserMap(Map<String,Object> map);
<!--对象中的属性,可以直接取出来 传递map的key-->
<insert id="insertUserMap" parameterType="map">
insert into mybatis.user (id,name,pwd) values (#{userId},#{userName},#{passWord});
</insert>
@Test
//使用map插入对象,适合于数据规模较大,需要插入的数据不规整
//或者使用在更新用户的信息上,不需要全部更新操作等
public void insertUserMap(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userId","3");
map.put("userName","");
map.put("passWord","19980812");
int i = mapper.insertUserMap(map);
if (i>0){
System.out.println("插入用户成功");
}
sqlSession.commit();
sqlSession.close();
}
Map传递参数,直接在sql中取出key即可! 【parameterType="map"】
对象传递参数,直接在sql中取对象的属性即可!【parameterType="Object"】
只有一个基本类型参数的情况下,可以直接在sql中取到!
多个参数用Map,或者注解!
8、思考题
模糊查询怎么写?
-
Java代码执行的时候,传递通配符 % % 比较安全 推荐使用
List<User> userList = mapper.getUserLike("%李%");
-
在sql拼接中使用通配符! 可能存在sql注意 不安全
<!-- select * from mybatis.user where id like =? select * from mybatis.user where id like =1 or 1=1--> select * from mybatis.user where name like "%"#{value}"%"
3.实现细节
- 编写接口
//模糊查询
List<User> getUserByLike(String name);
- 编写对应的Mapper.xml中的sql
<!-- select * from mybatis.user where name like #{name};-->
<select id="getUserByLike" resultType="com.loading.pojo.User" >
select * from mybatis.user where name like "%"#{name}"%";
</select>
- 编写对应的测试类
@Test
//模糊查询 根据关键字去查询
public void getUserByLike(){
//获取sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserByLike("直");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
结果如下:
User{id=1, name='直男', pwd='199868'}
User{id=2, name='直直', pwd='12345'}