1 创建一个初始模版(固定)
//sqlSessionFactory--->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//获取Mybatis第一步:获取sqlSessionFactory对象
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
//SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
2 CRUD
1.namespace
- namespace中的包名要和DAO/Mapper接口的包名一致(相当于一个实现接口的类)
2.select
选择查询语句 - id:就是对象的namespace中的方法名
- resultType:sql语句执行的返回值!(集合 要写全类名)
- parameterType:参数类型
- 一般步骤
- 编写接口
//根据id查询用户 User getUserById(int id);
- 编写对应的mapper中的sql
<select id="getUserById" resultType="com.ysj.pojo.User" parameterType="int"> select * from user where id = #{id} </select>
- 测试(增删改查都要提交事务)
SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.getUserById(3); sqlSession.commit(); sqlSession.close();
3 UserMapper
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据id查询用户
User getUserById(int id);
//insert一个用户
int addUser(User user);
//修改用户
int updateUser(User user);
//删除用户
void deleteUser(int id);
}
4 配置文件中接口的实现
<?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.ysj.dao.UserMapper">
<select id="getUserList" resultType="com.ysj.pojo.User">
select * from mybatis.user
</select>
<select id="getUserById" resultType="com.ysj.pojo.User" parameterType="int">
select * from user where id = #{id}
</select>
<!--对象中的属性,可以直接取出来-->
<insert id="addUser" parameterType="com.ysj.pojo.User">
insert into user (id,name,password) value (#{id},#{name},#{password})
</insert>
<update id="updateUser" parameterType="com.ysj.pojo.User" >
update user set name=#{name},password=#{password} where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
delete
from user
where id=#{id};
</delete>
</mapper>
5 错误分析
resource中不能使用点 只能使用/
- 标签不要匹配错
- 程序配置问价必须符合规范
- maven资源没有导出问题
- NullPointerException没有注册到资源
6 万能的map
假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用map1
//map增加用户
int addUser2(Map<String, Object> map);
<insert id="addUser2" parameterType="map">
insert into user (id,password)
values (#{userid},#{password});
</insert>
@Test
public void test7() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> map = new HashMap<>();
map.put("userid", 3);
map.put("password", "999999");
mapper.addUser2(map);
sqlSession.commit();
sqlSession.close();
}
- map传递参数,直接咋sql中取出key即可 [parameterType=“map”]
- 对象传递参数,直接在sql中取出对象即可 [parameterType=“Object”]
- 只有一个基本类型参数的情况下,直接在sql中取出即可
- 多个参数使用map,或者注解
7 模糊查询
//模糊查询用户
List<User> getUserLike(String value);
<!--模糊查询-->
<select id="getUserLike" parameterType="com.ysj.pojo.User">
select *
from user
where name like = #{value}
</select>
@Test
public void getUserLike() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserLike("%王%");
for (User user : userList) {
System.out.println(user);
}
sqlSession.commit();
sqlSession.close();
}