Skip to content

Commit

Permalink
update source code
Browse files Browse the repository at this point in the history
  • Loading branch information
abel533 committed Jul 6, 2017
1 parent 9a394ff commit c272a39
Show file tree
Hide file tree
Showing 88 changed files with 6,077 additions and 0 deletions.
57 changes: 57 additions & 0 deletions mybatis-spring-boot/pom.xml
@@ -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>
@@ -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();
}

}
@@ -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;
}

}

@@ -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!";
}
}
@@ -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();
}
}
@@ -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();
}
@@ -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;
}

}
@@ -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();
}
@@ -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 mybatis-spring-boot/src/main/resources/application.properties
@@ -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
@@ -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>

0 comments on commit c272a39

Please sign in to comment.