Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
happyxiaofan
committed
Sep 14, 2017
1 parent
50b5525
commit c503400
Showing
14 changed files
with
583 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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> | ||
|
||
<artifactId>spring-boot-mybatis-annotation</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<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> |
19 changes: 19 additions & 0 deletions
19
...atis-annotation/src/main/java/com/rhwayfun/springboot/mybatis/annotation/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.rhwayfun.springboot.mybatis.annotation; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* 程序入口 | ||
* | ||
* @author happyxiaofan | ||
* @since 0.0.1 | ||
*/ | ||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
SpringApplication.run(Application.class, args); | ||
Thread.sleep(Long.MAX_VALUE); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...ion/src/main/java/com/rhwayfun/springboot/mybatis/annotation/config/DataSourceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.rhwayfun.springboot.mybatis.annotation.config; | ||
|
||
import com.alibaba.druid.pool.DruidDataSource; | ||
import com.rhwayfun.springboot.mybatis.annotation.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.jdbc.datasource.DataSourceTransactionManager; | ||
|
||
import javax.sql.DataSource; | ||
|
||
/** | ||
* 数据源配置 | ||
* | ||
* @author happyxiaofan | ||
* @since 0.0.1 | ||
*/ | ||
@Configuration | ||
@ConfigurationProperties(prefix = DataSourceConstants.DATASOURCE_PREFIX) | ||
@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(DataSourceConstants.DRIVER_CLASS); | ||
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); | ||
/** | ||
* 这里不需要指定Mapper.xml的配置文件的路径,因为我们例子都是基于注解完成的,没有Mapper.xml配置文件 | ||
*/ | ||
/*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; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...c/main/java/com/rhwayfun/springboot/mybatis/annotation/constants/DataSourceConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.rhwayfun.springboot.mybatis.annotation.constants; | ||
|
||
/** | ||
* 数据源相关常量 | ||
* | ||
* @author happyxiaofan | ||
* @since 0.0.1 | ||
*/ | ||
public abstract class DataSourceConstants { | ||
|
||
/** | ||
* 数据源配置前缀 | ||
* | ||
*/ | ||
public static final String DATASOURCE_PREFIX = "mybatis.datasource"; | ||
|
||
/** | ||
* MySQL驱动 | ||
* | ||
*/ | ||
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"; | ||
|
||
/** | ||
* Mybatis Mapper接口路径 | ||
* | ||
*/ | ||
public static final String MAPPER_PACKAGE = "com.rhwayfun.springboot.mybatis.annotation.mapper"; | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...tion/src/main/java/com/rhwayfun/springboot/mybatis/annotation/constants/SqlConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.rhwayfun.springboot.mybatis.annotation.constants; | ||
|
||
/** | ||
* Sql语句 | ||
* | ||
* @author happyxiaofan | ||
* @since 0.0.1 | ||
*/ | ||
public abstract class SqlConstants { | ||
|
||
public static final String GET_ALL_SQL = "SELECT * FROM user"; | ||
|
||
public static final String GET_ONE_SQL = "SELECT * FROM user WHERE user_id = #{userId}"; | ||
|
||
public static final String INSERT_SQL = "INSERT INTO user(user_id,user_name,age,birth) VALUES(#{userId}, #{userName}, #{age}, #{birth})"; | ||
|
||
public static final String UPDATE_SQL = "UPDATE user SET user_name=#{userName},age=#{age},birth=#{birth} WHERE user_id =#{userId}"; | ||
|
||
public static final String DELETE_SQL = "DELETE FROM user WHERE user_id =#{userId}"; | ||
|
||
public static final String DELETE_ALL_SQL = "DELETE FROM user"; | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...n/src/main/java/com/rhwayfun/springboot/mybatis/annotation/constants/UserSqlProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.rhwayfun.springboot.mybatis.annotation.constants; | ||
|
||
import java.util.Map; | ||
|
||
import static org.apache.ibatis.jdbc.SqlBuilder.*; | ||
|
||
/** | ||
* Sql语句生成器 | ||
* | ||
* @author happyxiaofan | ||
* @since 0.0.1 | ||
*/ | ||
public class UserSqlProvider { | ||
|
||
private static final String TABLE_NAME = "user"; | ||
|
||
public String findOneSql(Map<String, Object> parameters) { | ||
Long userId = (Long) parameters.get("userId"); | ||
BEGIN(); | ||
SELECT("user_id, user_name, age, birth"); | ||
FROM(TABLE_NAME); | ||
if (userId != null) { | ||
WHERE("user_id = #{userId}"); | ||
} | ||
return SQL(); | ||
} | ||
|
||
public String findAllSql() { | ||
BEGIN(); | ||
SELECT("user_id, user_name, age, birth"); | ||
FROM(TABLE_NAME); | ||
return SQL(); | ||
} | ||
|
||
public String insertSql() { | ||
BEGIN(); | ||
INSERT_INTO(TABLE_NAME); | ||
VALUES("user_id", "#{userId}"); | ||
VALUES("user_name", "#{userName}"); | ||
VALUES("age", "#{age}"); | ||
VALUES("birth", "#{birth}"); | ||
return SQL(); | ||
} | ||
|
||
public String updateSql() { | ||
BEGIN(); | ||
UPDATE(TABLE_NAME); | ||
SET("user_name = #{userName}"); | ||
SET("age = #{age}"); | ||
SET("birth = #{birth}"); | ||
WHERE("user_id = #{userId}"); | ||
return SQL(); | ||
} | ||
|
||
public String deleteSql() { | ||
BEGIN(); | ||
DELETE_FROM(TABLE_NAME); | ||
WHERE("user_id = #{userId}"); | ||
return SQL(); | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...nnotation/src/main/java/com/rhwayfun/springboot/mybatis/annotation/entity/UserEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.rhwayfun.springboot.mybatis.annotation.entity; | ||
|
||
import java.io.Serializable; | ||
import java.util.Date; | ||
|
||
/** | ||
* 用户实体类,纯POJO | ||
* | ||
* @author happyxiaofan | ||
* @since 0.0.1 | ||
*/ | ||
public class UserEntity implements Serializable { | ||
|
||
private Integer id; | ||
|
||
private Long userId; | ||
|
||
private String userName; | ||
|
||
private Integer age; | ||
|
||
private Date birth; | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public Long getUserId() { | ||
return userId; | ||
} | ||
|
||
public void setUserId(Long userId) { | ||
this.userId = userId; | ||
} | ||
|
||
public String getUserName() { | ||
return userName; | ||
} | ||
|
||
public void setUserName(String userName) { | ||
this.userName = userName; | ||
} | ||
|
||
public Integer getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(Integer age) { | ||
this.age = age; | ||
} | ||
|
||
public Date getBirth() { | ||
return birth; | ||
} | ||
|
||
public void setBirth(Date birth) { | ||
this.birth = birth; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "UserEntity{" + | ||
"id=" + id + | ||
", userId=" + userId + | ||
", userName='" + userName + '\'' + | ||
", age=" + age + | ||
", birth=" + birth + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.