初學Spring真是傷不起呀,連一個Mybatis的配置都整了一天才弄出來,太不容易了,所以這裡一定要把它記錄下來,防止自已到時候又忘記了,雖然前後經歷了好長時間,但好在磕磕碰碰的弄出來了,也算給自已一點小安慰吧。 其實Mybatis的配置網上的資料真的不要太多,百度一下會找到好多,也許就是因為太多 ...
初學Spring真是傷不起呀,連一個Mybatis的配置都整了一天才弄出來,太不容易了,所以這裡一定要把它記錄下來,防止自已到時候又忘記了,雖然前後經歷了好長時間,但好在磕磕碰碰的弄出來了,也算給自已一點小安慰吧。
其實Mybatis的配置網上的資料真的不要太多,百度一下會找到好多,也許就是因為太多了,加上好多的文章都差不多,有時候想找點有用的東西還真不是那麼容易的事。而且,由於Mybatis版本的原因,找到的內容好多已經過時了,所以中間經歷的過程就不細說了,都是淚呀,當然可能也是因為我初學,菜鳥的原因吧,哎。
網上大部分能找到的 Mybatis的配置很多都是基於XML配置這種,那種也不是我想要的,我的整個Spring MVC工程也是用的Java config,就是沒有在web.xml或者其它的配置文件去配置Bean這種,所以,我的Mybatis也想用Java代碼的方式,這個就有一點不一樣了,所以在配置中遇到了一些問題。
Mybatis相關網站
像Maven、Spring MVC的配置過程這裡就不說了,這不是本文的重點,所以Mybatis的配置是在Spring框架配置好的基礎上進行的。
目錄結構
在配置之前,先看下整個項目的目錄結構
pom.xml的配置
需要在這個文件裡面增加Mybatis、Mybatis-spring、MySql的一些依賴
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.3.18.RELEASE</spring.version>
<spring.jdbc.version>5.2.0.RELEASE</spring.jdbc.version>
<mysql.connector.version>5.1.21</mysql.connector.version>
<servlet.version>3.0.1</servlet.version>
<jsp.version>2.2.1-b03</jsp.version>
<jstl.version>1.2</jstl.version>
<redis.version>2.0.3.RELEASE</redis.version>
<jedis.version>2.9.0</jedis.version>
<mybatis.version>3.5.4</mybatis.version>
<mybatis.spring.version>2.0.4</mybatis.spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- Servlet API 3.0 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.jdbc.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${redis.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
</dependencies>
再添加一個Mybatis的Bean配置,文件在 config/MyBatisConfig.java
package com.smartrui.common.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.sql.SQLException;
@Configuration
@PropertySource("classpath:config/application.properties")
@MapperScan(basePackages = {"com.smartrui.dao"})
public class MyBatisConfig {
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.initialSize}")
private int initialSize;
@Value("${spring.datasource.minIdle}")
private int minIdle;
@Value("${spring.datasource.maxActive}")
private int maxActive;
@Value("${spring.datasource.maxWait}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.filters}")
private String filters;
@Value("{spring.datasource.connectionProperties}")
private String connectionProperties;
@Value("${mybatis.type.alias.package}")
private String mybatisTypeAliasPackage;
@Bean //聲明其為Bean實例
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
@Bean
@Autowired
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage(mybatisTypeAliasPackage);
//指定mapper路徑地址
PathMatchingResourcePatternResolver classPathResource = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(classPathResource.getResources("classpath*:config/mappers/*.xml"));
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration() ;
sqlSessionFactoryBean.setConfiguration(configuration);
return sqlSessionFactoryBean;
}
}
上面的 SqlSessionFactoryBean
方法,主要有資料庫的配置
sqlSessionFactoryBean.setDataSource(dataSource);
和 mapper xml的路徑配置 sqlSessionFactoryBean.setMapperLocations
, 那個dataSource
可以配置數據源像Mysql、Oracle和其它的連接池配置等。
文件加好後,需要在WebConfig.java
這個文件中保證能掃描到,用 ComponentScan
或Import
的方式弄進去
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.smartrui.dao","com.smartrui.service","com.smartrui.controller"})
@Import({DataBaseConfig.class,MyBatisConfig.class})
public class WebAppConfig extends WebMvcConfigurerAdapter {
/**
* 靜態資源過濾
*/
@Override
public void addResourceHandlers( ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
}
....
註入SqlSessionFactory
新的MyBatis的sessionFactory載入有些不太一樣,需要指定下,我開始一直報SqlSessionFactory不存在,後面搜索了下,我在dao下建立一個公共的commonDAO,加上如下內容就可以了, 註意有 @Autowired
這個
package com.smartrui.dao;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
public class CommonDAO extends SqlSessionDaoSupport {
@Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
}
好了,增加上面的幾個文件後,Mybatis應該就算是配置好了,沒弄好之前感覺好難,其實做好後,你會發現就是那麼幾個配置就行了,接下來,來一個簡單的測試。
在 service
下麵寫一個簡單的查詢
package com.smartrui.service;
import org.apache.ibatis.annotations.Param;
public interface PersonService {
String getUserName(@Param("id") Integer id);
}
寫一個service
的實現
package com.smartrui.service.impl;
import com.smartrui.dao.PersonDAO;
import com.smartrui.dao.UserDAO;
import com.smartrui.model.User;
import com.smartrui.service.PersonService;
import com.smartrui.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("PersonService")
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonDAO personDAO ;
@Override
public String getUserName(Integer id) {
return personDAO.getUserName(id);
}
}
dao層加上介面
package com.smartrui.dao;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonDAO {
// @Select("SELECT user_name FROM tb_user WHERE id = #{id}")
String getUserName(@Param("id") Integer id);
}
在 config/mapper/PersonMapper.xml
裡面加上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.smartrui.dao.PersonDAO">
<select id="getUserName" resultType="String">
SELECT user_name FROM tb_user WHERE id = #{id}
</select>
</mapper>
上面有幾個註意的地方
dao
層裡面的方式如getUserName
要和PersonMapper.xml
裡面具體查詢里的id
值一樣。PersonMapper.xml
的namespace
的值,就是dao
層那個包的路徑。
上面的寫完後,在Controller
層增加一個service
的調用就行了
@RequestMapping("/getbyid")
public String getById(){
String name;
try {
name = personService.getUserName(11);
}catch(Exception e){
e.printStackTrace();
name = "N/A";
}
System.out.println("name:" + name);
return "user";
}
通過上面的配置就能把Mybatis搭建起來了,至於更深層原理性的東西,這個我暫時沒有時間去深究了,待後續框架用熟一些了再說吧,當前的目標就是能先用起來。