选择,查询语句:
<select id="getUserById" resultType="com.louis.pojo.User" parameterType="int">select>
id
:就是对应namespace中的方法名(就相当于重写了原来的方法)
resultType
:sql语句执行的返回值
parameterType
:表示参数类型,和传入的参数有关
namesepace中的包名要和Dao/mapper接口的包名一致。
UserDao接口
//根据id查询用户
User getUserById(Integer id);
UserMapper.xml
<select id="getUserById" resultType="com.louis.pojo.User" parameterType="int">
select * from mybatis.user where id = #{id};
select>
测试类
@Test
public void testUserById(){
SqlSession sqlSession = MybatisUtils.getsqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println(mapper.getUserById(1));
sqlSession.close();
}
UserDao接口
//insert一个用户
int addUser(User user);
UserMapper.xml
<insert id="addUser" parameterType="com.louis.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);
mapper.addUser(new User(4, "DC", "12wa"));
sqlSession.close();
}
通过上述方法我们可以执行成功,但在数据库中并没有插入相关的数据,这是事务导致。
解决方法
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getsqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int dc = mapper.addUser(new User(4, "DC", "12wa"));
//解决数据库中未插入的情况,增删改需要提交事务
if(dc > 0){
System.out.println("插入成功");
//提交
sqlSession.commit();
}
sqlSession.close();
}
UserDao接口
//修改用户
int updateUser(User user);
UserMapper.xml
<update id="updateUser" parameterType="com.louis.pojo.User">
update mybatis.user set name = #{name}, pwd = #{pwd} where id = #{id};
update>
测试类
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getsqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int i = userMapper.updateUser(new User(3, "微微", "Lzh"));
if(i > 0){
sqlSession.commit();
}
sqlSession.close();
}
UserDao接口
//删除用户
int deleteUser(int id);
UserMapper.xml
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id};
delete>
测试
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getsqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int result = userMapper.deleteUser(4);
if(result > 0){
sqlSession.commit();
}
sqlSession.close();
}
增删改在没有设置自动提交的时候需要手动提交事务。
假设我们的实体类或者数据库中的表字段或者参数过多,我们应当考虑使用map。
Map传递参数
:直接在sql中取出key即可(parameterType=“map”)
对象传递参数
:直接在sql中取对象的属性即可(parameterType=“com.louis.pojo.User”)
只有一个基本类型参数的情况下,可以直接在sql中取到。多个参数用Map,或者注解。
UserDao接口
//Map
int addUser2(Map<String, Object> map);
UserMapper.xml
<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);
Map<String, Object> map = new HashMap<>();
map.put("userId", 5);
map.put("userName", "咕噜咕噜");
map.put("password", "xzz");
int result = mapper.addUser2(map);
if(result > 0){
sqlSession.commit();
}
sqlSession.close();
}
UserDao接口
//模糊查询
List<User> selectUserLike(String value);
UserMapper.xml
<select id="selectUserLike">
select * from mybatis.user where name like #{value};
select>
测试类
@Test
public void selectUserLike(){
SqlSession sqlSession = MybatisUtils.getsqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.selectUserLike("%微%");
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
}
上面使用的方法存在sql注入的情况,优化如下:
UserMapper.xml
<select id="selectUserLike">
select * from mybatis.user where name like "%" #{value}"%";
select>
测试类
@Test
public void selectUserLike(){
SqlSession sqlSession = MybatisUtils.getsqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.selectUserLike("微");
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
}