1. pom.xml
包含了mybatis代码生成插件,c3p0连接池
4.0.0
com.itcuc
qaserver
0.0.1-SNAPSHOT
war
qaserver
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-tomcat
provided
org.springframework.boot
spring-boot-starter-test
test
com.mchange
c3p0
0.9.5.2
com.alibaba
fastjson
1.2.47
org.springframework.boot
spring-boot-starter-security
io.jsonwebtoken
jjwt
0.7.0
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
${basedir}/src/main/resources/generatorConfig.xml
true
true
2. 创建数据库user表
CREATE TABLE `t_sys_user` (
`id` char(36) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`mobile` varchar(11) DEFAULT NULL,
`login_ip` varchar(255) DEFAULT NULL,
`login_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`is_aviliable` tinyint(4) DEFAULT NULL,
`type` int(2) DEFAULT NULL,
`avatar` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `username` (`username`) USING HASH,
KEY `email` (`email`),
KEY `mobile` (`mobile`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
不需要的字段可以酌情删除,实际上可以只保留username和password字段
3. 创建generatorConfig.xml 用于自动生成mapper文件和模型
运行一下插件就可以生成代码了
4. 加入启动类
package com.itcuc.qaserver;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@SpringBootApplication
@MapperScan("com.itcuc.qaserver.mapper")
public class QaserverApplication {
public static void main(String[] args) {
SpringApplication.run(QaserverApplication.class, args);
}
}
5. 配置application.properties
server.port=8080
spring.datasource.name=qadata
spring.datasource.url=jdbc:mysql://localhost:3306/qa
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select 'x'
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=100
spring.datasource.maxOpenPreparedStatements=20
mybatis.type-aliases-package=com.itcuc.qaserver.model
mybatis.mapper-locations=classpath:mapping/*.xml
6. 创建DataSource和Sessionfactory
DataSourceConfiguration 类配置c3p0数据源
SessionFactoryConfiguration 类配置SqlSessionFactory
@SpringBootConfiguration
public class DataSourceConfiguration {
@Value("${spring.datasource.driver-class-name}")
private String jdbcDriver;
@Value("${spring.datasource.url}")
private String jdbcUrl;
@Value("${spring.datasource.username}")
private String jdbcUser;
@Value("${spring.datasource.password}")
private String jdbcPassword;
@Value("${spring.datasource.maxActive}")
private Integer maxPoolSize;
@Value("${spring.datasource.maxWait}")
private Integer maxIdleTime;
@Value("${spring.datasource.minIdle}")
private Integer minPoolSize;
@Value("${spring.datasource.poolPreparedStatements}")
private Integer maxStatements;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private Integer idleConnectionTestPeriod;
@Value("${spring.datasource.validationQuery}")
private String preferredTestQuery;
@Value("${spring.datasource.testOnBorrow}")
private Boolean testConnectionOnCheckin;
@Value("${spring.datasource.testOnReturn}")
private Boolean testConnectionOnCheckout;
@Bean
public DataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(jdbcDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(jdbcUser);
dataSource.setPassword(jdbcPassword);
dataSource.setMaxPoolSize(maxPoolSize);
dataSource.setMaxIdleTime(maxIdleTime);
dataSource.setMinPoolSize(minPoolSize);
dataSource.setMaxStatementsPerConnection(maxStatements);
dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
dataSource.setPreferredTestQuery(preferredTestQuery);
dataSource.setTestConnectionOnCheckin(testConnectionOnCheckin);
dataSource.setTestConnectionOnCheckout(testConnectionOnCheckout);
dataSource.setAutoCommitOnClose(false);
return dataSource;
}
}
@SpringBootConfiguration
public class SessionFactoryConfiguration {
@Value("${mybatis.mapper-locations}")
private String mapperXMLConfigPath;
@Value("${mybatis.type-aliases-package}")
private String mapperPackagePath;
@Autowired
private DataSource dataSource;
@Bean
public SqlSessionFactoryBean createSqlSessionFactory() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String packageXMLConfigPath = mapperXMLConfigPath;
// 设置mapper 对应的XML 文件的路径
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageXMLConfigPath));
// 设置数据源
sqlSessionFactoryBean.setDataSource(dataSource);
// 设置mapper 接口所在的包
sqlSessionFactoryBean.setTypeAliasesPackage(mapperPackagePath);
return sqlSessionFactoryBean;
}
}
7. 创建一个简单的api
@RestController
@RequestMapping("/api/index")
public class IndexController {
@GetMapping("hello")
public String hello() {
return "hello";
}
}
使用crap测试接口
参考感谢:
- https://blog.csdn.net/sxdtzhaoxinguo/article/details/77965226
- https://blog.csdn.net/codejas/article/details/79334545