Mybatis3集成Spring3Word格式文档下载.docx
- 文档编号:22832035
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:16
- 大小:376.21KB
Mybatis3集成Spring3Word格式文档下载.docx
《Mybatis3集成Spring3Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Mybatis3集成Spring3Word格式文档下载.docx(16页珍藏版)》请在冰豆网上搜索。
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){
publicintgetEmpno(){
returnempno;
publicvoidsetEmpno(intempno){
publicStringgetEname(){
returnename;
publicvoidsetEname(Stringename){
publicStringgetJob(){
returnjob;
publicvoidsetJob(Stringjob){
@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);
publicList<
Emp>
getAllEmp();
publicEmpgetById(Stringid);
publicvoiddeleteEmp(Stringid);
publicvoidupdateEmp(Map<
String,Object>
map);
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"
mappernamespace="
com.ribbonchen.mapper.EmpMapper"
<
parameterMaptype="
com.ribbonchen.entity.Emp"
id="
parameterMapEmp"
parameterproperty="
empno"
/>
ename"
job"
/parameterMap>
resultMaptype="
resultMapEmp"
resultproperty="
column="
/resultMap>
insertid="
insertEmp"
parameterMap="
INSERTINTOemp(empno,ename,job)
VALUES(?
?
)
/insert>
selectid="
getAllEmp"
resultMap="
SELECT*FROMemp
/select>
getById"
parameterType="
String"
WHEREempno=#{value}
deleteid="
deleteEmp"
DELETEFROMemp
/delete>
updateid="
updateEmp"
java.util.Map"
UPDATEemp
SETename=#{name},job=#{job}
WHEREempno=#{empno}
/update>
/mapper>
定义数据访问层接口及其实现
com.ribbonchen.dao.IEmpDao.java
packagecom.ribbonchen.dao;
publicinterfaceIEmpDao{
com.ribbonchen.dao.impl.EmpDaoImpl.java
packagecom.ribbonchen.dao.impl;
importcom.ribbonchen.dao.IEmpDao;
importcom.ribbonchen.mapper.EmpMapper;
publicclassEmpDaoImplimplementsIEmpDao{
privateEmpMapperempMapper;
//在此处注入一个empMapper
//这个empMapper由Spring自动生成,不需要我们自己手工去定义
publicvoidinsertEmp(Empemp){
this.empMapper.insertEmp(emp);
thrownewRuntimeException("
Error"
);
//测试抛出RuntimeException
////异常查看数据库是否存在记录
publicvoiddeleteEmp(Stringid){
this.empMapper.deleteEmp(id);
getAllEmp(){
returnthis.empMapper.getAllEmp();
publicEmpgetById(Stringid){
returnthis.empMapper.getById(id);
map){
this.empMapper.updateEmp(map);
publicEmpMappergetEmpMapper(){
returnempMapper;
publicvoidsetEmpMapper(EmpMapperempMapper){
this.empMapper=empMapper;
定义业务层接口及其实现
com.ribbonchen.service.IEmpSerivce.java
packagecom.ribbonchen.service;
publicinterfaceIEmpService{
com.ribbonchen.service.impl.EmpServiceImpl.java
packagecom.ribbonchen.service.impl;
importcom.ribbonchen.service.IEmpService;
publicclassEmpServiceImplimplementsIEmpService{
privateIEmpDaoempDao;
empDao.insertEmp(emp);
//thrownewRuntimeException("
publicIEmpDaogetEmpDao(){
returnempDao;
publicvoidsetEmpDao(IEmpDaoempDao){
this.empDao=empDao;
定义Spring配置文件
applicationContext.xml
beansxmlns="
//www.springframework.org/schema/beans"
xmlns:
xsi="
//www.w3.org/2001/XMLSchema-instance"
context="
//www.springframework.org/schema/context"
aop="
//www.springframework.org/schema/aop"
tx="
//www.springframework.org/schema/tx"
xsi:
schemaLocation="
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
//www.springframework.org/schema/context
//www.springframework.org/schema/context/spring-context-3.0.xsd
//www.springframework.org/schema/tx
//www.springframework.org/schema/tx/spring-tx-3.0.xsd
//www.springframework.org/schema/aop
//www.springframework.org/schema/aop/spring-aop-3.0.xsd"
---->
context:
annotation-config/>
component-scanbase-package="
com.ribbonchen.*"
/>
--jdbc.propertisDirectory-->
--bean
class="
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
<
propertyname="
locations"
value="
classpath:
jdbc.properties"
/bean-->
beanid="
MyDataSource"
destroy-method="
close"
mons.dbcp.BasicDataSource"
driverClassName"
com.mysql.jdbc.Driver"
url"
jdbc:
mysql:
//localhost:
3306/mybatis?
useUnicode=true&
amp;
characterEncoding=UTF-8"
username"
root"
password"
123456"
/bean>
--SqlSessionFactory-->
sqlSessionFactory"
class="
org.mybatis.spring.SqlSessionFactoryBean"
dataSource"
ref="
--ScanMapperFiles-->
beanclass="
org.mybatis.spring.mapper.MapperScannerConfigurer"
basePackage"
com.ribbonchen.mapper"
--
================================事务相关控制=================================================
-->
beanname="
transactionManager"
org.springframework.jdbc.datasource.DataSourceTransactionManager"
/property>
adviceid="
userTxAdvice"
transaction-manager="
attributes>
<
methodname="
delete*"
propagation="
REQUIRED"
read-only="
false"
rollback-for="
java.lang.Exception"
no-rollback-for="
java.lang.RuntimeException"
insert*"
update*"
find*"
SUPPORTS"
get*"
select*"
/tx:
advice>
aop:
config>
pointcutid="
pc"
expression="
execution(public*com.ribbonchen.service.*.*(..))"
--把事务控制在Service层-->
advisorpointcut-ref="
advice-ref="
/aop:
--以下为自定义Bean-->
empDao"
com.ribbonchen.dao.impl.EmpDaoImpl"
autowire="
byName"
empService"
com.ribbonchen.service.impl.EmpServiceImpl"
/beans>
定义log4j配置文件
log4j.xml此配置可让mybatis在运行过程中打印sql
DOCTYPElog4j:
configurationSYSTEM"
log4j.dtd"
log4j:
configurationxmlns:
log4j="
//jakarta.apache.org/log4j/"
--Consoleoutput-->
appendername="
STDOUT"
org.apache.log4j.ConsoleAppender"
layoutclass="
org.apache.log4j.PatternLayout"
paramname="
ConversionPattern"
%-5p%d{MM-ddHH:
mm:
ss}%m(%F:
%L)\n"
/layout>
/appender>
categoryname="
java.sql"
priorityvalue="
debug"
/category>
com.ibatis"
root>
error"
appender-refref="
/root>
/log4j:
configuration>
测试
packagecom.ribbonchen.test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassTest{
publ
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Mybatis3 集成 Spring3