简介
mybatis 是一个优秀的基于java的持久层框架,它内部封装了jdbc,使开发者只需要关注sql语句本身,而不需要花费精力去处理加载驱动、创建连接、创建statement等繁杂的过程。
mybatis通过xml或注解的方式将要执行的各种 statement配置起来,并通过java对象和statement中sql的动态参数进行映射生成最终执行的sql语句。
最后mybatis框架执行sql并将结果映射为java对象并返回。采用ORM思想解决了实体和数据库映射的问题,对jdbc 进行了封装,屏蔽了jdbc api 底层访问细节,使我们不用与jdbc api 打交道,就可以完成对数据库的持久化操作。
应用
传统方式
userMapper.xml
<mapper namespace="userMapper">
<!--查询所有-->
<select id="select" resultType="user">
select * from User
</select>
<insert id="insert" parameterType="user">
insert into user values (null ,#{username},#{password},null)
</insert>
<delete id="delete" parameterType="int">
delete from user where id=#{id}
</delete>
<update id="update" parameterType="int">
update user set username='wangwu' where id=#{id}
</update>
</mapper>
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--起别名-->
<typeAliases>
<typeAlias type="com.heqiang.domain.User" alias="user"></typeAlias>
</typeAliases>
<!--配置数据源环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///maven"/>
<property name="username" value="root"/>
<property name="password" value="myroot"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<mapper resource="com/com.heqiang/dao/userMapper.xml"/>
</mappers>
</configuration>
User
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String username;
private String password;
}
测试
@Test
//删除操作
public void test4() throws IOException {
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作 参数:namespace+id
sqlSession.delete("userMapper.delete",1);
//mybatis执行更新操作 需要手动提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
面向接口代理开发方式
介绍
Mapper 接口开发方法只需要程序员编写Mapper 接口(相当于Dao 接口),由Mybatis 框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。
遵循四个原则
1) Mapper.xml文件中的namespace与mapper接口的全限定名相同
2) Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3) Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
4) Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
PhgeHelp插件
1.导入坐标
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>1.0</version>
</dependency>
2.配置文件(properties)
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
3.使用方式
ublic List<User> findAll() {
PageHelper.startPage(1,5);
List<User> list = dao.findAll();
//获得与分页相关参数
PageInfo<User> pageInfo = new PageInfo<User>(list);
System.out.println("当前页数:" + pageInfo.getPageNum());
System.out.println("每页条数:" + pageInfo.getPageSize());
System.out.println("总条数:" + pageInfo.getTotal());
System.out.println("总页数:" + pageInfo.getPages());
System.out.println("上一页:" + pageInfo.getPrePage());
System.out.println("下一页:" + pageInfo.getNextPage());
System.out.println("是否是第一个:" + pageInfo.isIsFirstPage());
System.out.println("是否是最后一个:" + pageInfo.isIsLastPage());
return list;
}
多表查询
配置文件方式
一对一
<resultMap id="orderMap" type="order">
<!--主键单独有一个id-->
<id column="oid" property="id"></id>
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
<!--一对一第一种封装方式-->
<!--<result column="uid" property="user.id"></result>-->
<!--<result column="username" property="user.username"></result>-->
<!--<result column="password" property="user.password"></result>-->
<!--<result column="birthday" property="user.birthday"></result>-->
<!--一对一第二种封装方式-->
<!--property: 当前实体(order)中的属性名称(private User user)-->
<!--javaType: 当前实体(order)中的属性的类型(User)-->
<association property="user" javaType="user">
<id column="uid" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
</association>
</resultMap>
<select id="findAll" resultMap="orderMap">
SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
</select>
一对多
<resultMap id="userMap" type="user">
<id column="uid" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<!--配置集合信息
property:集合名称
ofType:当前集合中的数据类型
-->
<collection property="orderList" ofType="order">
<!--封装order的数据-->
<id column="oid" property="id"></id>
<result column="ordertime" property="ordertime"></result>
<result column="total" property="total"></result>
</collection>
</resultMap>
<select id="findAll" resultMap="userMap">
SELECT *,o.id oid FROM USER u,orders o WHERE u.id=o.uid
</select>
多对多
<resultMap id="userRoleMap" type="user">
<!--user的信息-->
<id column="userId" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="birthday" property="birthday"></result>
<!--user内部的roleList信息-->
<collection property="roleList" ofType="role">
<id column="roleId" property="id"></id>
<result column="roleName" property="roleName"></result>
<result column="roleDesc" property="roleDesc"></result>
</collection>
</resultMap>
<select id="findUserAndRoleAll" resultMap="userRoleMap">
SELECT * FROM USER u,sys_user_role ur,sys_role r WHERE u.id=ur.userId AND ur.roleId=r.id
</select>
注解方式
@Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
public void save(User user);
@Delete("delete from user where id=#{id}")
public void delete(int id);
@Update("update user set username=#{username},password=#{password} where id=#{id}")
public void update(User user);
@Select("select * from user where id=#{id}")
public User findById(int id);
一对一
@Select("select * from orders")
@Results({
@Result(column = "id", property = "id"),
@Result(column = "ordertime", property = "ordertime"),
@Result(column = "total", property = "total"),
@Result(
property = "user", //要封装的属性名称
column = "uid", //根据该字段去查询user表的数据
javaType = User.class, //要封装的实体类型
//select属性 代表查询那个接口的方法获得数据
one = @One(select = "com.itheima.dao.UserMapper.findById")
)
})
public List<Order> findAll();
一对多
@Select("select * from user")
@Results({
@Result(id = true, column = "id", property = "id"),
@Result(column = "username", property = "username"),
@Result(column = "password", property = "password"),
@Result(
property = "orderList",//封装User中的的属性名
column = "id",//根据什么再次查询
javaType = List.class,//
many = @Many(select = "com.itheima.dao.OrderMapper.findByUid")
)
})
public List<User> findUserAndOrderAll();
多对多
@Select("SELECT * FROM USER")
@Results({
@Result(id = true, column = "id", property = "id"),
@Result(column = "username", property = "username"),
@Result(column = "password", property = "password"),
@Result(
property = "roleList",
column = "id",
javaType = List.class,
many = @Many(select = "com.itheima.dao.RoleMapper.findByUid")
)
})
public List<User> findUserAndRoleAll();
注意事项
mybatis执行更新操作 需要手动提交事务