Mybatis3集成Spring3.docx
《Mybatis3集成Spring3.docx》由会员分享,可在线阅读,更多相关《Mybatis3集成Spring3.docx(16页珍藏版)》请在冰豆网上搜索。
Mybatis3集成Spring3
准备JAR包
请到官网下载Mybatis3.0.6和Spring3.0.6
另外,Spring还额外需要以下依赖jar包:
aspectjweaver.jar、aopalliance-1.0.jar、commons-dbcp-1.2.1.jar、commons-pool.jar;
其中aspectjweaver.jar是aspectj的一个jar包。
Mybatis结合Spring需要mybatis-spring.jar。
请用搜索引擎搜索jar包名字自行下载备用。
工程中引用的jar包如下图:
注意:
若后续测试过程中遇到以下运行时错误,则说明jar包未全,把上述jar包引入问题即解决:
java.lang.ClassNotFoundException:
mons.dbcp.BasicDataSource
java.lang.NoClassDefFoundError:
org/aopalliance/intercept/MethodInterceptor
Thematchingwildcardisstrict,butnodeclarationcanbefoundforelement'tx:
advice'.
数据库建表语句
DROPTABLEIFEXISTS`mybatis`.`emp`;
CREATETABLE`mybatis`.`emp`(
`empno`int(10)NOTNULLAUTO_INCREMENT,
`ename`varchar(30)NOTNULL,
`job`varchar(15)NOTNULL,
PRIMARYKEY(`empno`)
)ENGINE=InnoDBAUTO_INCREMENT=17DEFAULTCHARSET=utf8;
数据库类型为MySQL,表所在的数据库名为mybatis
工程目录结构
com.ribbonchen.dao定义数据访问层接口
com.ribbonchen.dao.impl定义数据访问层接口的实现
com.ribbonchen.entity定义mybatis用到的实体类
com.ribbonchen.mapper定义mybatis用到的数据操作接口和映射文件
com.ribbonchen.service定义业务层接口
com.ribbonchen.service.impl定义业务层接口的实现
com.ribbonchen.test定义测试类
另外,config文件夹里面是spring和log4j的配置文件
定义mybatis用到的实体类
com.ribbonchen.entity.Emp.java
publicclassEmp{
privateintempno;//员工编码
privateStringename;//员工名称
privateStringjob;//职位
publicEmp(){
}
publicEmp(intempno,Stringename,Stringjob){
this.empno=empno;
this.ename=ename;
this.job=job;
}
publicEmp(intempno){
this.empno=empno;
}
publicintgetEmpno(){
returnempno;
}
publicvoidsetEmpno(intempno){
this.empno=empno;
}
publicStringgetEname(){
returnename;
}
publicvoidsetEname(Stringename){
this.ename=ename;
}
publicStringgetJob(){
returnjob;
}
publicvoidsetJob(Stringjob){
this.job=job;
}
@Override
publicStringtoString(){
returnename+","+empno+","+job;
}
}
定义mybatis用到的数据操作接口和映射文件
com.ribbonchen.mapper.EmpMapper.java
packagecom.ribbonchen.mapper;
importjava.util.List;
importjava.util.Map;
importcom.ribbonchen.entity.Emp;
publicinterfaceEmpMapper{
publicvoidinsertEmp(Empemp);
publicListgetAllEmp();
publicEmpgetById(Stringid);
publicvoiddeleteEmp(Stringid);
publicvoidupdateEmp(Mapmap);
}
com.ribbonchen.mapper.EmpMapper.xml
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN"
"http:
//mybatis.org/dtd/mybatis-3-mapper.dtd">
INSERTINTOemp(empno,ename,job)
VALUES(?
?
?
)
SELECT*FROMemp
SELECT*FROMemp
WHEREempno=#{value}
DELETEFROMemp
WHEREempno=#{value}
UPDATEemp
SETename=#{name},job=#{job}
WHEREempno=#{empno}
定义数据访问层接口及其实现
com.ribbonchen.dao.IEmpDao.java
packagecom.ribbonchen.dao;
importjava.util.List;
importjava.util.Map;
importcom.ribbonchen.entity.Emp;
publicinterfaceIEmpDao{
publicvoidinsertEmp(Empemp);
publicListgetAllEmp();
publicEmpgetById(Stringid);
publicvoiddeleteEmp(Stringid);
publicvoidupdateEmp(Mapmap);
}
com.ribbonchen.dao.impl.EmpDaoImpl.java
packagecom.ribbonchen.dao.impl;
importjava.util.List;
importjava.util.Map;
importcom.ribbonchen.dao.IEmpDao;
importcom.ribbonchen.entity.Emp;
importcom.ribbonchen.mapper.EmpMapper;
publicclassEmpDaoImplimplementsIEmpDao{
privateEmpMapperempMapper;//在此处注入一个empMapper
//这个empMapper由Spring自动生成,不需要我们自己手工去定义
publicvoidinsertEmp(Empemp){
this.empMapper.insertEmp(emp);
thrownewRuntimeException("Error");//测试抛出RuntimeException
////异常查看数据库是否存在记录
}
publicvoiddeleteEmp(Stringid){
this.empMapper.deleteEmp(id);
}
publicListgetAllEmp(){
returnthis.empMapper.getAllEmp();
}
publicEmpgetById(Stringid){
returnthis.empMapper.getById(id);
}
publicvoidupdateEmp(Mapmap){
this.empMapper.updateEmp(map);
}
publicEmpMappergetEmpMapper(){
returnempMapper;
}
publicvoidsetEmpMapper(EmpMapperempMapper){
this.empMapper=empMapper;
}
}
定义业务层接口及其实现
com.ribbonchen.service.IEmpSerivce.java
packagecom.ribbonchen.service;
importcom.ribbonchen.entity.Emp;
publicinterfaceIEmpService{
publicvoidinsertEmp(Empemp);
}
com.ribbonchen.service.impl.EmpServiceImpl.java
packagecom.ribbonchen.service.impl;
importcom.ribbonchen.dao.IEmpDao;
importcom.ribbonchen.entity.Emp;
importcom.ribbonchen.service.IEmpService;
publicclassEmpServiceImplimplementsIEmpService{
privateIEmpDaoempDao;
publicvoidinsertEmp(Empemp){
empDao.insertEmp(emp);
//thrownewRuntimeException("Error");//测试抛出RuntimeException
}
publicIEmpDaogetEmpDao(){
returnempDao;
}
publicvoidsetEmpDao(IEmpDaoempDao){
this.empDao=empDao;
}
}
定义Spring配置文件
applicationContext.xml
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"xmlns:
context="http:
//www.springframework.org/schema/context"
xmlns:
aop="http:
//www.springframework.org/schema/aop"xmlns:
tx="http:
//www.springframework.org/schema/tx"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-3.0.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-3.0.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-3.0.xsd">
---->
annotation-config/>
component-scanbase-package="com.ribbonchen.*"/>
--jdbc.propertisDirectory-->
--bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
jdbc.properties"/>
class="mons.dbcp.BasicDataSource">
mysql:
//localhost:
3306/mybatis?
useUnicode=true&characterEncoding=UTF-8"/>
--SqlSessionFactory-->
--ScanMapperFiles-->
--
================================事务相关控制=================================================
-->
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
adviceid="userTxAdvice"transaction-manager="transactionManager">
attributes>
methodname="delete*"propagation="REQUIRED"read-only="false"
rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/>
methodname="insert*"propagation="REQUIRED"read-only="false"
rollback-for="java.lang.RuntimeException"/>
methodname="update*"propagation="REQUIRED"read-only="false"
rollback-for="java.lang.Exception"/>
methodname="find*"propagation="SUPPORTS"/>
methodname="get*"propagation="SUPPORTS"/>
methodname="select*"propagation="SUPPORTS"/>
attributes>
advice>
config>
pointcutid="pc"
expression="execution(public*com.ribbonchen.service.*.*(..))"/>
--把事务控制在Service层-->
advisorpointcut-ref="pc"advice-ref="userTxAdvice"/>
config>
--以下为自定义Bean-->
autowire="byName"/>
autowire="byName"/>
定义log4j配置文件
log4j.xml此配置可让mybatis在运行过程中打印sql
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPElog4j:
configurationSYSTEM"log4j.dtd">
configurationxmlns:
log4j="http:
//jakarta.apache.org/log4j/">
--Consoleoutput-->
mm:
ss}%m(%F:
%L)\n"/>
configuration>
测试
packagecom.ribbonchen.test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importcom.ribbonchen.entity.Emp;
importcom.ribbonchen.service.IEmpService;
publicclassTest{
publ