设计网站推荐提升审美,电商网站建设实验心得1500,重庆小程序开发公司,wordpress怎么降5.MyBatis的原始Dao开发-了解
使用Mybatis开发Dao#xff0c;通常有两个方法#xff0c;即原始Dao开发方式和Mapper接口代理开发方式。而现在主流的开发方式是接口代理开发方式#xff0c;这种方式总体上更加简便。我们的课程讲解也主要以接口代理开发方式为主。在第4节已经…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 {ListUser 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.sqlSessionsqlSession;}Overridepublic ListUser findAll() {return sqlSession.selectList(com.by.dao.UserDao.findAll);}Overridepublic User getUserById(Integer id) {return sqlSession.selectOne(com.by.dao.UserDao.getUserById,id);}
}
5.3.定义映射文件
?xml version1.0 encodingUTF-8?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
!--namespace隔离sql一般是接口名称的全类名--
mapper namespacecom.by.dao.UserDao!--id:和接口方法名保持一致resultType:和接口返回类型保持一致--select idfindAll resultTypecom.by.pojo.Userselect * from user/selectselect idgetUserById parameterTypejava.lang.Integer resultTypecom.by.pojo.Userselect * from user where id #{id}/select
/mapper5.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;Beforepublic void into() throws IOException {//加载配置文件String resource mybatis-config.xml;inputStream Resources.getResourceAsStream(resource);//创建sqlSessionFactorySqlSessionFactory sessionFactory new SqlSessionFactoryBuilder().build(inputStream);//获得数据的会话实例sqlSession sessionFactory.openSession();}Testpublic void testFindAll() throws IOException {UserDao userDao new UserDaoImpl(sqlSession);ListUser userList userDao.findAll();for (User user : userList) {System.out.println(user);}}Testpublic void testGetUserById() throws IOException {UserDao userDao new UserDaoImpl(sqlSession);System.out.println(userDao.getUserById(42));}Afterpublic void close() throws IOException {sqlSession.close();inputStream.close();}
}
6.MyBatis的ORM映射
拷贝01_mybatis_HelloWorld工程
6.1.什么是ORM映射
MyBatis只能自动维护库表”列名“与”属性名“相同时的对应关系二者不同时无法自动ORM如下
6.2.列的别名
在SQL中使用 as 为查询字段添加列别名以匹配属性名 public ListRole findAll();select idfindAll resultTypecom.by.pojo.Role select id, role_name as roleName, role_desc as roleDesc from role/select思考 如果我们的查询很多都使用别名的话写起来岂不是很麻烦有没有别的解决办法呢
6.3.结果映射
使用ResultMap标签手动映射解决实体字段和数据表字段不一致的问题
public ListRole findAll2();!--id:和select查询标签的返回值保持一致type: 映射实体的全类名--resultMap idfindAll2resultMap typecom.by.pojo.Role!--描述主键字段的映射关系property实体类的属性column数据表字段名称--id propertyid columnid/id!--描述非主键字段的映射关系property实体类的属性column数据表字段名称--result propertyroleName columnrole_name/resultresult propertyroleDesc columnrole_desc/result/resultMapselect idfindAll2 resultMapfindAll2resultMapselect * from role/select7.MyBatis的配置文件
拷贝01_mybatis_HelloWorld工程
7.1.properties标签
我们一般会把数据库配置信息定义在一个独立的配置文件里面比如db.properties
jdbc.drivercom.mysql.jdbc.Driver
jdbc.urljdbc:mysql://127.0.0.1:3306/mybatis?characterEncodingUTF-8
jdbc.usernameroot
jdbc.password1111那么我们如何在mybatis的核心配置文件里面加载外部的数据库配置信息呢? 2. 在SqlMapConfig.xml引入数据库配置信息
?xml version1.0 encodingUTF-8?
!DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtd
configurationproperties resourcedb.properties/propertiestypeAliasespackage namecom.by.pojo//typeAliases!--使用dev环境--environments defaultdev!--dev环境--environment iddevtransactionManager typeJDBC/transactionManager!--使用连接池中的数据源--dataSource typePOOLEDproperty namedriver value${jdbc.driver}/property nameurl value${jdbc.url}/property nameusername value${jdbc.username}/property namepassword value${jdbc.password}//dataSource/environment/environments!-- 扫描映射文件 --mapperspackage namecom.by.dao//mappers
/configuration7.2.typeAliases标签
查看mybatis源码可以看到 Mybatis 默认支持的别名 我们也可以为实体类定义别名提高书写效率
定义别名
?xml version1.0 encodingUTF-8?
!DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtd
configurationproperties resourcedb.properties/propertiestypeAliases!--定义单个别名--!--typeAlias typecom.by.pojo.User aliasUser/typeAlias--!--批量定义别名--package namecom.by.pojo/package/typeAliasesenvironments defaultmysqlenvironment idmysqltransactionManager typeJDBC/transactionManagerdataSource typePOOLEDproperty namedriver value${jdbc.driver}/property nameurl value${jdbc.url}/property nameusername value${jdbc.username}/property namepassword value${jdbc.password}//dataSource/environment/environmentsmappersmapper resourcecom/by/dao/UserDao.xml//mappers
/configuration使用别名
?xml version1.0 encodingUTF-8?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.by.dao.UserDao!--使用别名--select idfindAll resultTypeUserselect * from user/select
/mapper7.3.Mappers标签
Mappers标签的作用是用来在核心配置文件里面引入映射文件引入方式有如下三种 使用mapper映射文件的路径 mappersmapper resourcecom/by/dao/UserDao.xml//mappers使用mapper接口的路径 mappersmapper classcom.by.dao.UserDao/mapper
/mappers注意此种方法要求 mapper 接口名称和 mapper 映射文件名称相同 使用mapper接口的包名批量引入 mapperspackage namecom.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;Overridepublic 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 version1.0 encodingUTF-8?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.by.dao.AccountMapperresultMap idgetAccountByIdResultMap typeaccountid columnaid propertyid/idresult columnuid propertyuid/resultresult columnmoney propertymoney/result!--一对一查询使用association标签propertyuserAccount的属性名javaTypeuser等价于resultType--association propertyuser javaTypeuserid columnid propertyid/idresult columnusername propertyusername/resultresult columnbirthday propertybirthday/resultresult columnsex propertysex/resultresult columnaddress propertyaddress/result/association/resultMapselect idgetAccountById parameterTypeint resultMapgetAccountByIdResultMapSELECT a.id aid,a.uid uid,a.money money, u.* FROM account a LEFT JOIN user u on a.uidu.id WHERE a.id#{id}/select
/mapper8.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;Beforepublic void into() throws IOException {//加载配置文件String resource mybatis-config.xml;inputStream Resources.getResourceAsStream(resource);//创建sqlSessionFactorySqlSessionFactory sessionFactory new SqlSessionFactoryBuilder().build(inputStream);//获得数据的会话实例sqlSession sessionFactory.openSession();}Testpublic void testGetAccountById() throws IOException {AccountMapper accountMapper sqlSession.getMapper(AccountMapper.class);Account account accountMapper.getAccountById(1);System.out.println(account);}Afterpublic void close() throws IOException {sqlSession.close();inputStream.close();}
}