Skip to content

Commit

Permalink
新增spring-boot-mybatis案例
Browse files Browse the repository at this point in the history
  • Loading branch information
happyxiaofan committed Sep 12, 2017
1 parent 7aecdf2 commit 8b45b1a
Show file tree
Hide file tree
Showing 14 changed files with 1,453 additions and 17 deletions.
9 changes: 9 additions & 0 deletions docs/sql/springboot/spring-boot-mybatis.sql
@@ -0,0 +1,9 @@
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增,主键',
`user_id` bigint(20) DEFAULT NULL COMMENT '用户id',
`user_name` varchar(15) DEFAULT NULL,
`age` int(11) DEFAULT NULL COMMENT '年龄',
`birth` date DEFAULT NULL COMMENT '生日',
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户信息表';
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -416,14 +416,14 @@
</execution>
</executions>
</plugin>
<plugin>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.plugin.surefire.version}</version>
<configuration>
<argLine>@{argLine} -Xmx2048m</argLine>
</configuration>
</plugin>
</plugin>-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
Expand Down
68 changes: 57 additions & 11 deletions spring-boot-mybatis/pom.xml
@@ -1,15 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot-learning-examples</artifactId>
<groupId>com.rhwayfun</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-mybatis</artifactId>
<parent>
<artifactId>spring-boot-learning-examples</artifactId>
<groupId>com.rhwayfun</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>spring-boot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

</project>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,13 @@
package com.rhwayfun.springboot.mybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.class, args);
Thread.sleep(Long.MAX_VALUE);
}
}
@@ -0,0 +1,77 @@
package com.rhwayfun.springboot.mybatis.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.rhwayfun.springboot.mybatis.constants.DataSourceConstants;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@ConfigurationProperties(prefix = "mybatis.datasource")
@MapperScan(basePackages = { DataSourceConstants.MAPPER_PACKAGE }, sqlSessionFactoryRef = "mybatisSqlSessionFactory")
public class DataSourceConfig {

private String url;

private String username;

private String password;

@Bean(name = "mybatisDataSource")
public DataSource mybatisDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}

@Bean(name = "mybatisTransactionManager")
public DataSourceTransactionManager mybatisTransactionManager() {
return new DataSourceTransactionManager(mybatisDataSource());
}

@Bean(name = "mybatisSqlSessionFactory")
public SqlSessionFactory mybatisSqlSessionFactory(@Qualifier("mybatisDataSource") DataSource mybatisDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(mybatisDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(DataSourceConstants.MAPPER_LOCATION));
return sessionFactory.getObject();
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}
@@ -0,0 +1,8 @@
package com.rhwayfun.springboot.mybatis.constants;

public abstract class DataSourceConstants {

public static final String MAPPER_PACKAGE = "com.rhwayfun.springboot.mybatis.mapper";
public static final String MAPPER_LOCATION = "classpath:mybatis/*Mapper*.xml";

}
@@ -0,0 +1,179 @@
package com.rhwayfun.springboot.mybatis.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.id
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
private Integer id;

/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.user_id
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
private Long userId;

/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.user_name
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
private String userName;

/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.age
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
private Integer age;

/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.birth
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
private Date birth;

/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table user
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
private static final long serialVersionUID = 1L;

/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.id
*
* @return the value of user.id
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
public Integer getId() {
return id;
}

/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.id
*
* @param id the value for user.id
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
public void setId(Integer id) {
this.id = id;
}

/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.user_id
*
* @return the value of user.user_id
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
public Long getUserId() {
return userId;
}

/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.user_id
*
* @param userId the value for user.user_id
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
public void setUserId(Long userId) {
this.userId = userId;
}

/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.user_name
*
* @return the value of user.user_name
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
public String getUserName() {
return userName;
}

/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.user_name
*
* @param userName the value for user.user_name
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}

/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.age
*
* @return the value of user.age
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
public Integer getAge() {
return age;
}

/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.age
*
* @param age the value for user.age
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
public void setAge(Integer age) {
this.age = age;
}

/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.birth
*
* @return the value of user.birth
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
public Date getBirth() {
return birth;
}

/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.birth
*
* @param birth the value for user.birth
*
* @mbg.generated Tue Sep 12 20:40:14 CST 2017
*/
public void setBirth(Date birth) {
this.birth = birth;
}
}

0 comments on commit 8b45b1a

Please sign in to comment.