博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring整合MyBatis
阅读量:7145 次
发布时间:2019-06-29

本文共 5400 字,大约阅读时间需要 18 分钟。

一、参考文档(参考此文档后成功整合)

 
二、Spring、mysql数据库、MyBatis版本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!--① 依赖的Spring模块类库 -->
        
<
dependency
>
            
<
groupId
>org.springframework</
groupId
>
            
<
artifactId
>spring-context</
artifactId
>
            
<
version
>3.1.1.RELEASE</
version
>
        
</
dependency
>
        
<
dependency
>
            
<
groupId
>org.springframework</
groupId
>
            
<
artifactId
>spring-beans</
artifactId
>
            
<
version
>3.1.1.RELEASE</
version
>
        
</
dependency
>
        
<
dependency
>
            
<
groupId
>org.springframework</
groupId
>
            
<
artifactId
>spring-web</
artifactId
>
            
<
version
>3.1.1.RELEASE</
version
>
        
</
dependency
>
        
<
dependency
>
            
<
groupId
>org.springframework</
groupId
>
            
<
artifactId
>spring-webmvc</
artifactId
>
            
<
version
>3.1.1.RELEASE</
version
>
        
</
dependency
>
        
<
dependency
>
            
<
groupId
>org.springframework</
groupId
>
            
<
artifactId
>spring-test</
artifactId
>
            
<
version
>3.1.1.RELEASE</
version
>
        
</
dependency
>
        
<
dependency
>
            
<
groupId
>org.springframework</
groupId
>
            
<
artifactId
>spring-jdbc</
artifactId
>
            
<
version
>3.1.1.RELEASE</
version
>
        
</
dependency
>
        
<
dependency
>
            
<
groupId
>org.springframework</
groupId
>
            
<
artifactId
>spring-orm</
artifactId
>
            
<
version
>3.1.1.RELEASE</
version
>
        
</
dependency
>
 
<
dependency
>
            
<
groupId
>commons-dbcp</
groupId
>
            
<
artifactId
>commons-dbcp</
artifactId
>
            
<
version
>1.4</
version
>
        
</
dependency
>
        
<
dependency
>
            
<
groupId
>mysql</
groupId
>
            
<
artifactId
>mysql-connector-java</
artifactId
>
            
<
version
>5.1.34</
version
>
        
</
dependency
>
      
        
<!-- 数据源 -->
        
<
dependency
>
            
<
groupId
>org.mybatis</
groupId
>
            
<
artifactId
>mybatis</
artifactId
>
            
<
version
>3.1.1</
version
>
        
</
dependency
>
        
<
dependency
>
            
<
groupId
>org.mybatis</
groupId
>
            
<
artifactId
>mybatis-spring</
artifactId
>
            
<
version
>1.2.0</
version
>
        
</
dependency
>

 

三、配置文件(均在resources文件夹中)

1、spring-mvc.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
""
    
xmlns:xsi
=
""
    
xmlns:context
=
""
    
xmlns:mvc
=
""
    
xmlns:p
=
""
    
xmlns:tx
=
""
    
xmlns:aop
=
""
    
xsi:schemaLocation="
        
        
        
        
        
        
        
        
        
        
"
        
default-init-method
=
"init"
        
>
 
    
<!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
    
<
mvc:annotation-driven
/>
 
    
<
context:component-scan
base-package
=
"com.fly"
/>
 
    
<!-- 引入jdbc配置文件 -->
    
<
context:property-placeholder
location
=
"classpath:jdbc.properties"
/>
    
<!--创建jdbc数据源 -->
    
<
bean
id
=
"dataSource"
class
=
"org.apache.commons.dbcp.BasicDataSource"
        
destroy-method
=
"close"
>
        
<
property
name
=
"driverClassName"
value
=
"${jdbc.driverClassName}"
/>
        
<
property
name
=
"url"
value
=
"${jdbc.url}"
/>
        
<
property
name
=
"username"
value
=
"${jdbc.username}"
/>
        
<
property
name
=
"password"
value
=
"${jdbc.password}"
/>
    
</
bean
>
    
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    
<
bean
id
=
"transactionManager"
        
class
=
"org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
        
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
    
</
bean
>
 
    
<!-- 创建SqlSessionFactory,同时指定数据源 -->
    
<
bean
id
=
"sqlSessionFactory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
        
<
property
name
=
"configLocation"
value
=
"classpath:mybatis.xml"
></
property
>
        
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
    
</
bean
>
 
    
<!-- 可通过注解控制事务 -->
    
<
tx:annotation-driven
transaction-manager
=
"transactionManager"
/>
 
    
<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
    
<
bean
class
=
"org.mybatis.spring.mapper.MapperScannerConfigurer"
>
        
<
property
name
=
"basePackage"
value
=
"com.fly.dao"
/>
    
</
bean
>
</
beans
>

 

 

2、jdbc.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#数据库连接
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:
//127.0.0.1:3306/hr?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
jdbc.minPoolSize=
5
jdbc.maxPoolSize=
20
jdbc.maxIdleTime=
1800
jdbc.acquireIncrement=
2
jdbc.maxStatements=
50
jdbc.initialPoolSize=
10
jdbc.idleConnectionTestPeriod=
1800
jdbc.acquireRetryAttempts=
2
cpool.automaticTestTable=Test
cpool.preferredTestQuery=select
1
from Test
cpool.testConnectionOnCheckin=
true

3、mybatis.xml(mapper节点对应xml文件)

1
2
3
4
5
6
7
8
9
10
11
12
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?> 
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD  Config 3.0//EN" "">
    
<
configuration
    
<!-- <typeAliases> 
        
<typeAlias alias="Student" type="com.matol.entity.User" /> 
    
</typeAliases>   -->
        
    
<
mappers
        
<
mapper
resource
=
"com/fly/dao/UserMapper.xml"
/> 
    
</
mappers
</
configuration
>

 

四、Dao层

1、Java文件

1
2
3
4
5
6
7
package
com.fly.dao;
import
org.springframework.stereotype.Repository;
 
@Repository
public
interface
UserMapper {
    
Integer count();
}

 

2、xml文件(xml中的namespace对应java文件的完整目录)

1
2
3
4
5
6
7
8
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "">
 
<
mapper
namespace
=
"com.fly.dao.UserMapper"
>
    
<
select
id
=
"count"
resultType
=
"Integer"
>
        
SELECT count(*) FROM operatelog
    
</
select
>
</mapper

 

五、Service层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package
com.fly.service;
 
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Service;
 
import
com.fly.dao.UserMapper;
 
@Service
public
class
UserService {
 
    
@Autowired
    
private
UserMapper userMapper;
 
    
public
int
count() {
        
return
this
.userMapper.count();
    
}
}

 

 

 

 

 

 

 

转载地址:http://cagrl.baihongyu.com/

你可能感兴趣的文章
苹果允许Flash程序在iPad和iPhone中使用
查看>>
一起谈.NET技术,XML与DataSet对象的关系
查看>>
艾伟_转载:【译】12个asp.net MVC最佳实践
查看>>
MySQL索引
查看>>
flask/sqlalchemy - OperationalError: (sqlite3.OperationalError) no such table
查看>>
每个势利鬼都有一副奴才相
查看>>
LINQ to Object的一个例子
查看>>
在CI框架中如何实现伪静态
查看>>
ORACLE Postgresql中文排序
查看>>
UBOOT到内核到文件系统设置需要注意点
查看>>
卡尔曼滤波简介——4.方差比较
查看>>
mysql -- 预处理语句
查看>>
Silverlight如何调用淘宝API
查看>>
ESP8266- AP模式的使用
查看>>
hdu 1503 LCS输出路径【dp】
查看>>
博客园开张第一天
查看>>
java绘图
查看>>
The Semantic Web, Linked Data and Open Data
查看>>
用PHP逐行读取TXT文件
查看>>
从Android中Activity之间的通信说开来[转]
查看>>