從頭開始搭建一個mybatis+postgresql平臺
最近有個項目的數據庫使用postgresql,使用原生態的mybatis操作數據,原生態的沒什么不好,只不過國內有個tk.mybatis的工具幫助我們做了很多實用的事情,大多數情況下我們需要在原生態mybatis上加工的想法它基本上都已經有很好的實現,這篇將分享安裝postgresql,配置tk.mybatis的詳細步驟以及在這過程中可能遇到的一些小問題。
安裝postgresql,執行下面的命令就可以安裝了:
服務端安裝好之后我們還需要一個圖形界面的客戶端pdAdmin,我安裝的是Windows版本的postgresql自帶的,可以到這個地址找對應的版本。安裝成功后默認會創建一個系統用戶,一個數據庫用戶,名稱以及密碼都是postgres,我們可以新創建用戶也可以直接使用這個帳號,反正我這只是測試。安裝完成之后,可能會遇到遠程訪問問題:
遠程連接問題,默認情況下只允許本地連接,要想允許其它客戶端連接,我們可以修改它的配置文件,這個文件的目錄位于/etc/postgresql/9.5/main,這個目錄下有兩個文件:
1:postgresql.conf,這個是服務器相關,里面有一個listen_address的地址,默認只監聽本地,我們可以修改它。
2:pg_hba.cof,這個是用戶權限相關,里面有一個與連接相關的配置,可以配置成網關模式
成功連接之后,大概是這個樣子,我們可以創建數據庫,表等對象。
mybatis代碼生成器,數據庫與Model的映射,這類機械的工作應該交給機器來完成,詳細使用參考這里。
通用mapper,單表的CRUD操作可以抽像出一個公共接口,tk.mybatis提供的通用mapper可以幫助我們解決這類問題。
----mapper.xml,足夠小(只包含字段映射)
1
2
3
4
5
6
7
8
9
|
< mapper namespace = "com.jim.logstashmvc.dao.generated.mapper.ProductMapper" > < resultMap id = "BaseResultMap" type = "com.jim.logstashmvc.dao.generated.entity.Product" > <!-- WARNING - @mbggenerated --> < id column = "id" jdbcType = "BIGINT" property = "id" /> < result column = "name" jdbcType = "VARCHAR" property = "name" /> </ resultMap > </ mapper > |
----mapper,足夠簡單(只需要繼承通過mapper接口)
插件,這里有分頁插件,SQL性能分析插件等,與mybatis集成非常容易。
如何與spring集成?
生成器的集成,可以采用maven方式來運行代碼生成器
依賴的包
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
|
<!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- Spring集成 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- MBG --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${MBG.version}</version> <scope>compile</scope> <optional> true </optional> </dependency> <!-- 分頁 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- 通用Mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> <!-- TkMybatis 會使用到JPA的注解 --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version> 1.0 </version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version> 9.3 - 1102 -jdbc41</version> </dependency> |
配置生成器插件,指定配置文件路徑,配置依賴:一個是數據庫驅動,一個是通用mapper
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
|
<!--MBG--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${MBG.version}</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite> true </overwrite> <verbose> true </verbose> </configuration> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version> 9.3 - 1102 -jdbc41</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> </dependencies> </plugin> |
生成器配置文件
**配置數據庫連接
**配置生成的model,mapper以及mapper.xml的存放路徑
**配置需要生成的表信息
注意下targetRuntime,這里采用的是MyBatis3Simple,它的默認選項是MyBatis3。如果采用通用mapper,我們在spring掃描接口時可以這樣寫。
如果是MyBatis3,生成的mapper.xml格式會復雜很多,我之前遇到過這樣的問題:使用MyBatis3生成的mapper.xml然后錯誤 的配置了MapperScannerConfigurer為下面通用mapper模式,提示我的錯誤如下,原因可以認定是配置問題(不是某個mapper.xml中的id重復問題) ,后續再研究下非通用mapper的配置。
生成器的配置詳細如下:
配置maven運行參數,如下圖所示即可。
mybatis的集成,主要是配置連接池信息,插件,mapper掃描等信息。
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
|
<bean id= "jimDataSource" class = "org.apache.commons.dbcp.BasicDataSource" > <property name= "driverClassName" value= "${jdbc.driverClass}" /> <property name= "url" value= "${jdbc.url}" /> <property name= "username" value= "${jdbc.username}" /> <property name= "password" value= "${jdbc.password}" /> <property name= "initialSize" value= "5" /> <property name= "minIdle" value= "10" /> <property name= "maxWait" value= "60000" /> <property name= "timeBetweenEvictionRunsMillis" value= "60000" /> <property name= "minEvictableIdleTimeMillis" value= "3600000" /> <property name= "validationQuery" value= "SELECT 1" /> <property name= "testWhileIdle" value= "true" /> <property name= "testOnBorrow" value= "false" /> <property name= "testOnReturn" value= "false" /> </bean> <bean id= "jimSqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "jimDataSource" /> <property name= "mapperLocations" value= "classpath:mapper/*.xml" /> <property name= "typeAliasesPackage" value= "com.jim.logstashmvc.dao.generated.entity" /> <property name= "plugins" > <array> <bean class = "com.github.pagehelper.PageHelper" > <property name= "properties" > <value> dialect=postgresql reasonable= true supportMethodsArguments= true returnPageInfo=check params=count=countSql </value> </property> </bean> </array> </property> </bean> <bean class = "tk.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "sqlSessionFactoryBeanName" value= "jimSqlSessionFactory" /> <property name= "basePackage" value= "com.jim.logstashmvc.dao.generated.mapper" /> </bean> |
通用mapper的用法:
• mapper,所有生成的mapper就繼承于Mapper<T>,默認持有通用mapper所有接口,包含CRUD常見操作
• IService,通用mapper接口的定義,我們可以根據自己的業務修改此接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Service public interface IService<T> { T selectByKey(Object key); int save(T entity); int delete(Object key); int updateAll(T entity); int updateNotNull(T entity); List<T> selectByExample(Object example); //TODO 其他... } |
BaseService,通用mapper的實現類
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
|
public abstract class BaseService<T> implements IService<T> { @Autowired protected Mapper<T> mapper; public Mapper<T> getMapper() { return mapper; } @Override public T selectByKey(Object key) { return mapper.selectByPrimaryKey(key); } public int save(T entity) { return mapper.insert(entity); } public int delete(Object key) { return mapper.deleteByPrimaryKey(key); } public int updateAll(T entity) { return mapper.updateByPrimaryKey(entity); } public int updateNotNull(T entity) { return mapper.updateByPrimaryKeySelective(entity); } public List<T> selectByExample(Object example) { return mapper.selectByExample(example); } //TODO 其他... } |
具體服務類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Service public class ProductServiceImpl extends BaseService<Product> implements ProductService { @Override public List<Product> selectByProduct(Product product, int page, int rows) { Example example = new Example(Product. class ); Example.Criteria criteria = example.createCriteria(); if (!StringUtils.isBlank(product.getName())){ criteria.andEqualTo( "name" ,product.getName()); } if (product.getId() != null ) { criteria.andEqualTo( "id" , product.getId()); } PageHelper.startPage(page, rows); return selectByExample(example); } } |
安裝postgresql并且成功遠程連接,集成MBG生成mapper以及model,然后將mybatis與spring集成,最后通過通用mapper中聯起來就達到了我們的目的:通過少量的代碼完成大部分的工作,重復勞動交給工具完成。但通用mapper有它的優點也就有它的缺點,需要根據項目環境來平衡,個人感覺利大于弊。
本文引用:
1、http://www.mybatis.tk/
2、https://github.com/abel533/Mybatis-Spring
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。