MyBatis与Spring的整合(二)
一.Spring整合MyBatis的准备工作
文章目录
1.创建数据库、表并添加数据
CREATE DATABASE smbm;
USE `smbms`;
DROP TABLE IF EXISTS `smbms_provider`;
CREATE TABLE `smbms_provider` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`proCode` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '供应商编码',
`proName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '供应商名称',
`proDesc` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '供应商详细描述',
`proContact` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '供应商联系人',
`proPhone` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '联系电话',
`proAddress` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '地址',
`proFax` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '传真',
`createdBy` bigint(20) DEFAULT NULL COMMENT '创建者(userId)',
`creationDate` datetime DEFAULT NULL COMMENT '创建时间',
`modifyDate` datetime DEFAULT NULL COMMENT '更新时间',
`modifyBy` bigint(20) DEFAULT NULL COMMENT '更新者(userId)',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*Data for the table `smbms_provider` */
insert into `smbms_provider`(`id`,`proCode`,`proName`,`proDesc`,`proContact`,`proPhone`,`proAddress`,
`proFax`,`createdBy`,`creationDate`,`modifyDate`,`modifyBy`)
values (1,'BJ_GYS001','北京三木堂商贸有限公司','长期合作伙伴,主营产品:茅台、五粮液、郎酒、酒鬼酒、泸州老窖、赖茅酒、法国红酒等','张国强','13566667777','北京市丰台区育芳园北路','010-58858787',1,'2013-03-21 16:52:07',NULL,NULL),
(2,'HB_GYS001','石家庄帅益食品贸易有限公司','长期合作伙伴,主营产品:饮料、水饮料、植物蛋白饮料、休闲食品、果汁饮料、功能饮料等','王军','13309094212','河北省石家庄新华区','0311-67738876',1,'2016-04-13 04:20:40',NULL,NULL),
(3,'GZ_GYS001','深圳市泰香米业有限公司','初次合作伙伴,主营产品:良记金轮米,龙轮香米等','郑程瀚','13402013312','广东省深圳市福田区深南大道6006华丰大厦','0755-67776212',1,'2014-03-21 16:56:07',NULL,NULL),
(4,'GZ_GYS002','深圳市喜来客商贸有限公司','长期合作伙伴,主营产品:坚果炒货.果脯蜜饯.天然花茶.营养豆豆.特色美食.进口食品.海味零食.肉脯肉','林妮','18599897645','广东省深圳市福龙工业区B2栋3楼西','0755-67772341',1,'2013-03-22 16:52:07',NULL,NULL),
(5,'JS_GYS001','兴化佳美调味品厂','长期合作伙伴,主营产品:天然香辛料、鸡精、复合调味料','徐国洋','13754444221','江苏省兴化市林湖工业区','0523-21299098',1,'2015-11-22 16:52:07',NULL,NULL),
(6,'BJ_GYS002','北京纳福尔食用油有限公司','长期合作伙伴,主营产品:山茶油、大豆油、花生油、橄榄油等','马莺','13422235678','北京市朝阳区珠江帝景1号楼','010-588634233',1,'2012-03-21 17:52:07',NULL,NULL),
(7,'BJ_GYS003','北京国粮食用油有限公司','初次合作伙伴,主营产品:花生油、大豆油、小磨油等','王驰','13344441135','北京大兴青云店开发区','010-588134111',1,'2016-04-13 00:00:00',NULL,NULL),
(8,'ZJ_GYS001','慈溪市广和绿色食品厂','长期合作伙伴,主营产品:豆瓣酱、黄豆酱、甜面酱,辣椒,大蒜等农产品','薛圣丹','18099953223','浙江省宁波市慈溪周巷小安村','0574-34449090',1,'2013-11-21 06:02:07',NULL,NULL),
(9,'GX_GYS001','优百商贸有限公司','长期合作伙伴,主营产品:日化产品','李立国','13323566543','广西南宁市秀厢大道42-1号','0771-98861134',1,'2013-03-21 19:52:07',NULL,NULL),
(10,'JS_GYS002','南京火头军信息技术有限公司','长期合作伙伴,主营产品:不锈钢厨具等','陈女士','13098992113','江苏省南京市浦口区浦口大道1号新城总部大厦A座903室','025-86223345',1,'2013-03-25 16:52:0,NULL,NULL),
(11,'GZ_GYS003','广州市白云区美星五金制品厂','长期合作伙伴,主营产品:海绵床垫、坐垫、靠垫、海绵枕头、头枕等','梁天','13562276775','广州市白云区钟落潭镇福龙路20号','020-85542231',1,'2016-12-21 06:12:17',NULL,NULL),
(12,'BJ_GYS004','北京隆盛日化科技','长期合作伙伴,主营产品:日化环保清洗剂,家居洗涤专卖、洗涤用品网、墙体除霉剂、墙面霉菌清除剂等','孙欣','13689865678','北京市大兴区旧宫','010-35576786',1,'2014-11-21 12:51:11',NULL,NULL),
(13,'SD_GYS001','山东豪克华光联合发展有限公司','长期合作伙伴,主营产品:洗衣皂、洗衣粉、洗衣液、洗洁精、消杀类、香皂等','吴洪转','13245468787','山东济阳济北工业区仁和街21号','0531-53362445',1,'2015-01-28 10:52:07',NULL,NULL),
(14,'JS_GYS003','无锡喜源坤商行','长期合作伙伴,主营产品:日化品批销','周一清','18567674532','江苏无锡盛岸西路','0510-32274422',1,'2016-04-23 11:11:11',NULL,NULL),
(15,'ZJ_GYS002','乐摆日用品厂','长期合作伙伴,主营产品:各种中、高档塑料杯,塑料乐扣水杯(密封杯)、保鲜杯(保鲜盒)、广告杯、礼品杯','王世杰','13212331567','浙江省金华市义乌市义东路','0579-34452321',1,'2016-08-22 10:01:30',NULL,NULL);
2.创建maven项目,加入Spring、Mybatis及整合相关的jar文件
[详细步骤如下]
(https://blog.csdn.net/Smile_xiaoyan/article/details/82975482)
在pom.xml编写代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<groupId>com.bdqn.Spring_MyBatis2</groupId>
<artifactId>Spring_MyBatis2</artifactId>
<version>1.0-SNAPSHOT</version>
<!--db-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- log4j -->
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<!--扫描到xml文件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
3.建立开发目录结构,创建实体类
目录结构,如下图
首先在com.bqdn下创建4个包,分别是entity,dao,service,mapper,
在entity目录下创建实体类Provider
package com.bdqn.entity;
import javax.swing.plaf.SliderUI;
import java.util.Date;
/**
* 供应商类
*/
public class Provider {
private int id; //主键id
private String proCode; //供应商编码
private String proName; //供应商名称
private String proDesc; //供应商详细描述
private String proContact; //供应商联系人
private String proPhone; //联系电话
private String proAddress; //地址
private String proFax; //传真
private long createdBy; //创建者(userId)
private Date creationDate; //创建时间
private Date modifyDate; //更新时间
private long modifyBy; //更新者
public Provider() {
}
public Provider(int id, String proCode, String proName, String proDesc, String proContact, String proPhone, String proAddress, String proFax, long createdBy, Date creationDate, Date modifyDate, long modifyBy) {
this.id = id;
this.proCode = proCode;
this.proName = proName;
this.proDesc = proDesc;
this.proContact = proContact;
this.proPhone = proPhone;
this.proAddress = proAddress;
this.proFax = proFax;
this.createdBy = createdBy;
this.creationDate = creationDate;
this.modifyDate = modifyDate;
this.modifyBy = modifyBy;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProCode() {
return proCode;
}
public void setProCode(String proCode) {
this.proCode = proCode;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public String getProDesc() {
return proDesc;
}
public void setProDesc(String proDesc) {
this.proDesc = proDesc;
}
public String getProContact() {
return proContact;
}
public void setProContact(String proContact) {
this.proContact = proContact;
}
public String getProPhone() {
return proPhone;
}
public void setProPhone(String proPhone) {
this.proPhone = proPhone;
}
public String getProAddress() {
return proAddress;
}
public void setProAddress(String proAddress) {
this.proAddress = proAddress;
}
public String getProFax() {
return proFax;
}
public void setProFax(String proFax) {
this.proFax = proFax;
}
public long getCreatedBy() {
return createdBy;
}
public void setCreatedBy(long createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public long getModifyBy() {
return modifyBy;
}
public void setModifyBy(long modifyBy) {
this.modifyBy = modifyBy;
}
@Override
public String toString() {
return "Provider{" +
"id=" + id +
", proCode='" + proCode + '\'' +
", proName='" + proName + '\'' +
", proDesc='" + proDesc + '\'' +
", proContact='" + proContact + '\'' +
", proPhone='" + proPhone + '\'' +
", proAddress='" + proAddress + '\'' +
", proFax='" + proFax + '\'' +
", createdBy=" + createdBy +
", creationDate=" + creationDate +
", modifyDate=" + modifyDate +
", modifyBy=" + modifyBy +
'}';
}
}
4.创建数据访问接口和实现类
在dao目录下创建接口ProviderDao
package com.bdqn.dao;
import com.bdqn.entity.Provider;
import java.util.List;
public interface ProviderDao {
//查询供应商信息
public List<Provider> getList();
}在这里插入代码片
在dao目录下创建一个包Impl
在Impl目录下创建一个数据访问接口的实现类ProviderDaoImpl
package com.bdqn.dao.Impl;
import com.bdqn.dao.ProviderDao;
import com.bdqn.entity.Provider;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class ProviderDaoImpl implements ProviderDao{
private SqlSessionTemplate sqlSessionTemplate;
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
public List<Provider> getList() {
return sqlSessionTemplate.selectList("com.bdqn.dao.ProviderDao.getList");
}
}
5.创建服务层的接口和实现类
在service目录下创建ProviderService
package com.bdqn.service;
import com.bdqn.entity.Provider;
import java.util.List;
public interface ProviderService {
public List<Provider> getList();
}
在service目录下创建一个包Impl
在Impl目录下创建一个服务层接口的实现类ProviderServiceImpl
package com.bdqn.service.Impl;
import com.bdqn.dao.ProviderDao;
import com.bdqn.entity.Provider;
import com.bdqn.service.ProviderService;
import java.util.List;
/**
* 实现类
*/
public class ProviderServiceImpl implements ProviderService {
//Dao层的对象
private ProviderDao providerDao;
public ProviderDao getProviderDao() {
return providerDao;
}
public void setProviderDao(ProviderDao providerDao) {
this.providerDao = providerDao;
}
public List<Provider> getList() {
return providerDao.getList();
}
}
6.配置SQL映射语句文件
在mapper目录下创建ProviderMapper.xml
<?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.bdqn.dao.ProviderDao">
<select id="getList" resultType="Provider">
select * from smbms_provider;
</select>
</mapper>
7.配置MyBatis应用配置文件
在resource目录下创建mybatis_config.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>
<package name="com.bdqn.entity"/>
</typeAliases>
</configuration
8.配置Spring应用配置文件
在resource目录下创建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--dataSource-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/smbms"/>
<property name="username" value="root"/>
<property name="password" value="000000"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引用数据源组件-->
<!--ref指引用对象-->
<property name="dataSource" ref="dataSource"/>
<!--引用MyBatis配置文件中的配置-->
<property name="configLocation" value="classpath:mybatis_config.xml"/>
<!--配置SQL映射文件信息-->
<property name="mapperLocations">
<list>
<value>classpath:com/bdqn/mapper/**/*.xml</value>
</list>
</property>
</bean>
<!--SqlSessionTmplate没有无参构造方法,也没有sqlSessionFactory,只能使用构造注入-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
<!--Dao层-->
<bean id="providerDao" class="com.bdqn.dao.Impl.ProviderDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
<!--Service层-->
<bean id="providerService" class="com.bdqn.service.Impl.ProviderServiceImpl">
<property name="providerDao" ref="providerDao"/>
</bean>
</beans>
9.测试类
测试类代码
import com.bdqn.entity.Provider;
import com.bdqn.service.ProviderService;
import org.apache.logging.log4j.LogManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test {
public static void main(String[] args) {
org.apache.logging.log4j.Logger logger= LogManager.getLogger(Test.class);
//读取applicationContext.xml
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ProviderService ps=(ProviderService)context.getBean("providerService");
List<Provider> list=ps.getList();
for (Provider p:list){
System.out.println(p);
}
}
}
执行结果:
注意:
1.映射文件的命名空间namespace=“命名空间是接口的包名,接口名”
2.映射文件中seclect的id=“数据访问层接口的方法名”
3.注意项目中的数据库名与表名是否与数据库的一样
4.创建SqlSessionTemplate 实例时,需要通过其构造方法注入SqlSessionF actory主.这里引用的是前文配置过的id为sqlSessionFactory的Bean。
5.与MyBatis 中默认的SqlSession 实现不同,SqlSessionTemplate是线程安全的可以单例模式配置并被多个DAO对象共用(单例模式会在后续课程中介绍),而不必为生个DAO单独配置一个SqlSessionTemplate
6.逐个列出所有的SOL映射文件比较烦琐,在SqlSessionFactoryBean 的配置中可以使用mapperLocations属性扫描式加载SQL映射文件
其中“classpath:com/bdqn/mapper/**/*.xml”表示扫com.bdqn.mapper包及其任意层级子包中,任意名称的xml类型的文件。
补充:
异常:Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.bdqn.dao.ProviderDao.getList
解决方法:
1.检查namespace是否是接口的包名.接口名
2.需要在pom.xml添加,如下
<!--扫描xml文件-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>