增加(Create)、检索(Retrieve)、更新(Update)和删除(Delete)
1、namespace
namespace中的包名要和dao/mapper接口的包名一致
2、select
选择,查询语句:
<?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">
<!--命名空间=绑定的一个对应的Dao/Mapper接口-->
<mapper namespace="com.linfeng.dao.UserMapper">
<select id="getUserList" resultType="com.linfeng.pojo.user">
select * from mybatis.user;
</select>
<!-- #{}取变量-->
<select id="getUserByid" parameterType="int" resultType="com.linfeng.pojo.user">
select *from mybatis.user where id = #{id}
</select>
<insert id="addUser" parameterType="com.linfeng.pojo.user">
insert into mybatis.user (id, name, pwd) value(#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="com.linfeng.pojo.user">
update mybatis.user
set name =#{name},pwd=#{pwd}
where id = #{id};
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id =#{id};
</delete>
</mapper>
在这里插入代码片package com.linfeng.dao;
import com.linfeng.pojo.user;
import com.linfeng.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
//获得sqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//执行
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<user> userList = mapper.getUserList();
for (user user : userList) {
System.out.println(user);
}
//关闭sqlsession
sqlSession.close();
}
@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();
}
@Test
//增删改需要提交事务
public void addUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.addUser(new user(4, "baby", "123444"));
if (i > 0) {
System.out.println("插入成功");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new user(4,"hhh","123444"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
}
package com.linfeng.dao;
import com.linfeng.pojo.user;
import java.util.List;
public interface UserMapper {
//查询全部用户
List<user> getUserList();
//根据id查询用户
user getUserByid(int i);
//insert用户
int addUser(user user);
//修改用户
int updateUser (user user);
//删除用户
int deleteUser(int id);
}
假设,我们的实例类,或者数据库中的表,字段或者参数过多,我们应该考虑使用map!
接口
int addUser2(Map map);
mapper
insert into `user`(id, name, pwd) values (#{userId},#{userName},#{password})
Map传递参数,直接在sql中取出key即可!
对象传递参数,直接在sql中取对象的属性即可!
只有一个基本类型的情况下,可以直接在sql中取到!
多个参数用Map,或者注解!
public void addUser2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userid",5);
map.put("userName","hello");
map.put("password","123333");
mapper.addUser2(map);//传一个map
sqlSession.close();
}
java代码执行的时候,传递通配符%
List<User> userList = userMapper.getUserLike("%z%");
在sql中拼接使用通配符 (不推荐,容易引起sql注入问题)
<!-- select id, name, pwd from `user` where id = ? -->
<!-- select id, name, pwd from `user` where id = 1 一般情况-->
<!-- select id, name, pwd from `user` where id = 1 or 1=1 sql注入情况-->
<select id="getUserLike" resultType="com.zyy.pojo.User" parameterType="string">
select id, name, pwd from `user` where name like "%"#{value}"%"
</select>