forked from mybatis-book/book
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
88 changed files
with
6,077 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.github.abel533.springboot</groupId> | ||
<artifactId>mybatis-spring-boot</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.4.3.RELEASE</version> | ||
</parent> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-jdbc</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>1.2.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>tk.mybatis</groupId> | ||
<artifactId>simple</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
35 changes: 35 additions & 0 deletions
35
mybatis-spring-boot/src/main/java/tk/mybatis/springboot/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,35 @@ | ||
package tk.mybatis.springboot; | ||
|
||
import org.mybatis.spring.annotation.MapperScan; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
import tk.mybatis.springboot.mapper.CountryMapper; | ||
|
||
/** | ||
* Spring Boot 启动类 | ||
*/ | ||
@SpringBootApplication | ||
@MapperScan(value = { | ||
"tk.mybatis.springboot.mapper", | ||
"tk.mybatis.simple.mapper" | ||
}, | ||
nameGenerator = MapperNameGenerator.class | ||
) | ||
public class Application implements CommandLineRunner { | ||
|
||
@Autowired | ||
private CountryMapper countryMapper; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
countryMapper.selectAll(); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
mybatis-spring-boot/src/main/java/tk/mybatis/springboot/MapperNameGenerator.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,37 @@ | ||
package tk.mybatis.springboot; | ||
|
||
import java.beans.Introspector; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.beans.factory.support.BeanNameGenerator; | ||
import org.springframework.util.ClassUtils; | ||
|
||
/** | ||
* Mapper 名字生成器 | ||
*/ | ||
public class MapperNameGenerator implements BeanNameGenerator { | ||
Map<String, Integer> nameMap = new HashMap<String, Integer>(); | ||
|
||
@Override | ||
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { | ||
//获取类的名字,如 CountryMapper | ||
String shortClassName = ClassUtils.getShortName(definition.getBeanClassName()); | ||
//将类名转换为规范的变量名,如 countryMapper | ||
String beanName = Introspector.decapitalize(shortClassName); | ||
//判断名字是否已经存在,如果存在,则在名字后面增加序号 | ||
if(nameMap.containsKey(beanName)){ | ||
int index = nameMap.get(beanName) + 1; | ||
nameMap.put(beanName, index); | ||
//增加序号 | ||
beanName += index; | ||
} else { | ||
nameMap.put(beanName, 1); | ||
} | ||
return beanName; | ||
} | ||
|
||
} | ||
|
13 changes: 13 additions & 0 deletions
13
mybatis-spring-boot/src/main/java/tk/mybatis/springboot/controller/IndexController.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,13 @@ | ||
package tk.mybatis.springboot.controller; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class IndexController { | ||
|
||
@RequestMapping("/") | ||
String home(){ | ||
return "Hello World!"; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
mybatis-spring-boot/src/main/java/tk/mybatis/springboot/controller/UserController.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,28 @@ | ||
package tk.mybatis.springboot.controller; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import tk.mybatis.simple.model.SysUser; | ||
import tk.mybatis.springboot.service.UserService; | ||
|
||
@RestController | ||
public class UserController { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@RequestMapping("users/{id}") | ||
SysUser user(@PathVariable("id") Long id){ | ||
return userService.findById(id); | ||
} | ||
|
||
@RequestMapping("users") | ||
List<SysUser> users(){ | ||
return userService.findAll(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
mybatis-spring-boot/src/main/java/tk/mybatis/springboot/mapper/CountryMapper.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,17 @@ | ||
package tk.mybatis.springboot.mapper; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.ibatis.annotations.Mapper; | ||
|
||
import tk.mybatis.springboot.model.Country; | ||
|
||
@Mapper | ||
public interface CountryMapper { | ||
/** | ||
* 查询全部数据 | ||
* | ||
* @return | ||
*/ | ||
List<Country> selectAll(); | ||
} |
32 changes: 32 additions & 0 deletions
32
mybatis-spring-boot/src/main/java/tk/mybatis/springboot/model/Country.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,32 @@ | ||
package tk.mybatis.springboot.model; | ||
|
||
public class Country { | ||
private Long id; | ||
private String countryname; | ||
private String countrycode; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getCountryname() { | ||
return countryname; | ||
} | ||
|
||
public void setCountryname(String countryname) { | ||
this.countryname = countryname; | ||
} | ||
|
||
public String getCountrycode() { | ||
return countrycode; | ||
} | ||
|
||
public void setCountrycode(String countrycode) { | ||
this.countrycode = countrycode; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
mybatis-spring-boot/src/main/java/tk/mybatis/springboot/service/UserService.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 tk.mybatis.springboot.service; | ||
|
||
import java.util.List; | ||
|
||
import tk.mybatis.simple.model.SysUser; | ||
|
||
public interface UserService { | ||
|
||
/** | ||
* 通过 id 查询用户 | ||
* | ||
* @param id | ||
* @return | ||
*/ | ||
SysUser findById(Long id); | ||
|
||
/** | ||
* 查询全部用户 | ||
* | ||
* @return | ||
*/ | ||
List<SysUser> findAll(); | ||
} |
28 changes: 28 additions & 0 deletions
28
mybatis-spring-boot/src/main/java/tk/mybatis/springboot/service/impl/UserServiceImpl.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,28 @@ | ||
package tk.mybatis.springboot.service.impl; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import tk.mybatis.simple.mapper.UserMapper; | ||
import tk.mybatis.simple.model.SysUser; | ||
import tk.mybatis.springboot.service.UserService; | ||
|
||
@Service | ||
public class UserServiceImpl implements UserService { | ||
|
||
@Autowired | ||
private UserMapper userMapper; | ||
|
||
@Override | ||
public SysUser findById(Long id) { | ||
return userMapper.selectById(id); | ||
} | ||
|
||
@Override | ||
public List<SysUser> findAll() { | ||
return userMapper.selectAll(); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
mybatis-spring-boot/src/main/resources/application.properties
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,11 @@ | ||
#spring.datasource.url=jdbc:mysql://localhost:3306/mybatis | ||
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis | ||
spring.datasource.username=root | ||
spring.datasource.password= | ||
|
||
logging.level.root=WARN | ||
logging.level.tk.mybatis.springboot.mapper=TRACE | ||
logging.level.tk.mybatis.simple.mapper=TRACE | ||
|
||
mybatis.mapper-locations=classpath:mapper/*.xml,classpath*:tk/**/mapper/*.xml | ||
mybatis.typeAliasesPackage=tk.mybatis.springboot.model |
7 changes: 7 additions & 0 deletions
7
mybatis-spring-boot/src/main/resources/mapper/CountryMapper.xml
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,7 @@ | ||
<?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="tk.mybatis.springboot.mapper.CountryMapper"> | ||
<select id="selectAll" resultType="Country"> | ||
select id,countryname,countrycode from country | ||
</select> | ||
</mapper> |
Oops, something went wrong.