Mapper.xml文件中的 字段属性解析
<mapper namespace="com.lsw.dao.UserDao">
<select id="getListUser" resultType="com.lsw.pojo.User">
select * from user;
</select>
</mapper>
namespace
:命名空间- id:就是对应的
namespace
中的方法名; - resultType:Sql语句执行的返回值;
- parameterType:参数类型
CRUD操作
查询
Mapper接口
// 查询所有的用户
List<User> getUserList();
// 根据id 查询用户
User getUserById(Integer id);
// Insert 一个用户
int addUser(User user);
// upadta 用户
int updataUser(User user);
// 删除一个用户
int deleteUser(int id);
编写对应的Mapper中的 SQL 语句
<select id="getUserList" resultType="com.lsw.pojo.User">
select * from user
</select>
<select id="getUserById" parameterType="Integer" resultType="com.lsw.pojo.User">
select * from 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();
}
inster插入操作
//增删改需要提交事务
sqlSession.commit();
不过可以在工具类中设置自动提交事务
<insert id="addUser" parameterType="com.lsw.pojo.User" >
insert into user (id, name, pwd) value (#{id}, #{name}, #{pwd});
</insert>
//增删改需要提交事务
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.addUser(new User(6, "lushuwei", "123333"));
if (res>0){
System.out.println("插入成功。。。");
}
// 提交事务
sqlSession.commit();
sqlSession.close();
}
update 更新操作
<update id="updataUser" parameterType="com.lsw.pojo.User">
update user set name = #{name},pwd = #{pwd} where id = #{id} ;
</update>
@Test
public void updataUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updataUser(new User(4, "江南", "000000"));
sqlSession.commit();
sqlSession.close();
}
Delete 删除操作
<delete id="deleteUser" parameterType="com.lsw.pojo.User">
delete from user where id = #{id};
</delete>
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
三、Map和模糊查询
多参数—Map
假设,我们的实体类,或者数据库中的表,字段或者 参数过多,我们应当考虑 Map!
Map 传递参数,直接在SQL中取出 key 即可!【parameterType=“map”】
对象传递参数,直接在SQL取对象的属性即可【parameterType=“com.lsw.pojo.User”】
只有一个基本类型参数的情况下,可以直接在SQL中取出。【可以不写】
多个参数 可以使用Map 或者 注解
<select id="getUserById" resultType="com.lsw.pojo.User">
select * from user where id = #{id};
</select>
借助万能的Map来实现插入操作
int addUser2(Map<String, Object> map);
<insert id="addUser2" parameterType="map">
insert into user (id, name, pwd) value (#{userid}, #{username}, #{userpwd});
</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", 7);
map.put("username", "南宫仆射");
map.put("userpwd", "123123");
mapper.addUser2(map);
sqlSession.close();
}
模糊查询
// 模糊查询
List<User> getUserLike( String value);
<select id="getUserLike" resultType="com.lsw.pojo.User">
select * from user where name like #{value};
</select>
java 代码执行的时候,传递通配符%value%
@Test
public void getUserLike(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> user = mapper.getUserLike("%李%");
for (User item : user){
System.out.println(item);
}
sqlSession.close();
}
也可以使用 SQL 的原生拼接语句【拼接通配符】
select * from user where name like "%"#{value}"%";