5.MyBatis的原始Dao开发-了解
使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方式和Mapper接口代理开发方式。而现在主流的开发方式是接口代理开发方式,这种方式总体上更加简便。我们的课程讲解也主要以接口代理开发方式为主。在第4节已经给大家介绍了基于代理方式的dao开发,现在给大家介绍一下基于传统编写Dao实现类的开发方式。
拷贝01_mybatis_HelloWorld工程:
5.1.创建接口
package com.by.dao;
import com.by.pojo.User;
import java.util.List;
public interface UserDao {
List<User> findAll();
User getUserById(Integer id);
}
5.2.创建接口实现类
package com.by.dao;
import com.by.pojo.User;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class UserDaoImpl implements UserDao{
private SqlSession sqlSession;
public UserDaoImpl(SqlSession sqlSession){
this.sqlSession=sqlSession;
}
@Override
public List<User> findAll() {
return sqlSession.selectList("com.by.dao.UserDao.findAll");
}
@Override
public User getUserById(Integer id) {
return sqlSession.selectOne("com.by.dao.UserDao.getUserById",id);
}
}
5.3.定义映射文件
<?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:隔离sql,一般是接口名称的全类名-->
<mapper namespace="com.by.dao.UserDao">
<!--
id:和接口方法名保持一致
resultType:和接口返回类型保持一致
-->
<select id="findAll" resultType="com.by.pojo.User">
select * from user
</select>
<select id="getUserById" parameterType="java.lang.Integer" resultType="com.by.pojo.User">
select * from user where id = #{id}
</select>
</mapper>
5.4.测试
package com.by.test;
import com.by.dao.UserDao;
import com.by.dao.UserDaoImpl;
import com.by.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyBatisTest{
private SqlSession sqlSession;
private InputStream inputStream;
@Before
public void into() throws IOException {
//加载配置文件
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
//创建sqlSessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获得数据的会话实例
sqlSession = sessionFactory.openSession();
}
@Test
public void testFindAll() throws IOException {
UserDao userDao = new UserDaoImpl(sqlSession);
List<User> userList = userDao.findAll();
for (User user : userList) {
System.out.println(user);
}
}
@Test
public void testGetUserById() throws IOException {
UserDao userDao = new UserDaoImpl(sqlSession);
System.out.println(userDao.getUserById(42));
}
@After
public void close() throws IOException {
sqlSession.close();
inputStream.close();
}
}
6.MyBatis的ORM映射
拷贝01_mybatis_HelloWorld工程:
6.1.什么是ORM映射
MyBatis只能自动维护库表”列名“与”属性名“相同时的对应关系,二者不同时无法自动ORM,如下:
6.2.列的别名
在SQL中使用 as 为查询字段添加列别名,以匹配属性名:
public List<Role> findAll();
<select id="findAll" resultType="com.by.pojo.Role" >
select id, role_name as roleName, role_desc as roleDesc from role
</select>
思考: 如果我们的查询很多,都使用别名的话写起来岂不是很麻烦,有没有别的解决办法呢?
6.3.结果映射
使用ResultMap
标签手动映射,解决实体字段和数据表字段不一致的问题
public List<Role> findAll2();
<!--
id:和select查询标签的返回值保持一致
type: 映射实体的全类名
-->
<resultMap id="findAll2resultMap" type="com.by.pojo.Role">
<!--
描述主键字段的映射关系:
property:实体类的属性
column:数据表字段名称
-->
<id property="id" column="id"></id>
<!--
描述非主键字段的映射关系:
property:实体类的属性
column:数据表字段名称
-->
<result property="roleName" column="role_name"></result>
<result property="roleDesc" column="role_desc"></result>
</resultMap>
<select id="findAll2" resultMap="findAll2resultMap">
select * from role
</select>
7.MyBatis的配置文件
拷贝01_mybatis_HelloWorld工程:
7.1.properties标签
- 我们一般会把数据库配置信息定义在一个独立的配置文件里面,比如db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=1111
那么我们如何在mybatis的核心配置文件里面加载外部的数据库配置信息呢?
2. 在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>
<properties resource="db.properties"></properties>
<typeAliases>
<package name="com.by.pojo"/>
</typeAliases>
<!--使用dev环境-->
<environments default="dev">
<!--dev环境-->
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<!--使用连接池中的数据源-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 扫描映射文件 -->
<mappers>
<package name="com.by.dao"/>
</mappers>
</configuration>
7.2.typeAliases标签
查看mybatis源码可以看到 Mybatis 默认支持的别名:
我们也可以为实体类定义别名,提高书写效率:
- 定义别名
<?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>
<properties resource="db.properties"></properties>
<typeAliases>
<!--定义单个别名-->
<!--<typeAlias type="com.by.pojo.User" alias="User"></typeAlias>-->
<!--批量定义别名-->
<package name="com.by.pojo"></package>
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/by/dao/UserDao.xml"/>
</mappers>
</configuration>
- 使用别名
<?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">
<mapper namespace="com.by.dao.UserDao">
<!--使用别名-->
<select id="findAll" resultType="User">
select * from user
</select>
</mapper>
7.3.Mappers标签
Mappers标签的作用是用来在核心配置文件里面引入映射文件,引入方式有如下三种:
-
使用mapper映射文件的路径
<mappers> <mapper resource="com/by/dao/UserDao.xml"/> </mappers>
-
使用mapper接口的路径
<mappers> <mapper class="com.by.dao.UserDao"></mapper> </mappers>
注意:此种方法要求 mapper 接口名称和 mapper 映射文件名称相同
-
使用mapper接口的包名批量引入
<mappers> <package name="com.by.dao"></package> </mappers>
注意:此种方法要求 mapper 接口名称和 mapper 映射文件名称相同
8.MyBatis的关联查询
拷贝06_mybatis_Config工程:
8.1.什么是关联查询
-
实体间的关系(拥有 has、属于 belong)
-
OneToOne:一对一关系(account ←→ user)
-
OneToMany:一对多关系(user ←→ account)
-
ManyToMany:多对多关系(user ←→ role)
-
-
什么是关联查询
当访问关系的一方时,如果需要查看与之关联的另一方数据,则必须使用表链接查询,将查询到的另一方数据,保存在本方的属性中
-
关联查询的语法
指定“一方”关系时(对象),使用
< association javaType="" >
指定“多方”关系时(集合),使用
< collection ofType="" >
8.2.一对一查询
需求:查询账户信息,关联查询用户信息。
分析:因为一个账户信息只能供某个用户使用,所以从查询账户信息出发关联查询用户信息为一对一查询。
8.2.1.pojo
package com.by.pojo;
// 一个
public class Account {
private Integer id;
private Integer uid;
private Double money;
// 一个
private User user;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
", user=" + user +
'}';
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}
8.2.2.mapper
package com.by.dao;
import com.by.pojo.Account;
public interface AccountMapper {
Account getAccountById(Integer id);
}
<?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">
<mapper namespace="com.by.dao.AccountMapper">
<resultMap id="getAccountByIdResultMap" type="account">
<id column="aid" property="id"></id>
<result column="uid" property="uid"></result>
<result column="money" property="money"></result>
<!--
一对一查询使用association标签
property="user":Account的属性名
javaType="user":等价于resultType
-->
<association property="user" javaType="user">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="birthday" property="birthday"></result>
<result column="sex" property="sex"></result>
<result column="address" property="address"></result>
</association>
</resultMap>
<select id="getAccountById" parameterType="int" resultMap="getAccountByIdResultMap">
SELECT a.id aid,a.uid uid,a.money money, u.* FROM account a LEFT JOIN user u on a.uid=u.id WHERE a.id=#{id}
</select>
</mapper>
8.2.3.测试
package com.by.test;
import com.by.dao.AccountMapper;
import com.by.dao.UserDao;
import com.by.pojo.Account;
import com.by.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyBatisTest{
private SqlSession sqlSession;
private InputStream inputStream;
@Before
public void into() throws IOException {
//加载配置文件
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
//创建sqlSessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获得数据的会话实例
sqlSession = sessionFactory.openSession();
}
@Test
public void testGetAccountById() throws IOException {
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
Account account = accountMapper.getAccountById(1);
System.out.println(account);
}
@After
public void close() throws IOException {
sqlSession.close();
inputStream.close();
}
}