前人总结的最佳实践案例可以帮助我们免去很多不必要的麻烦。要学会站在巨人人肩膀上看待问题;
项目环境:
插件:
步骤:
1.创建一个springboot项目:
2.创建项目的文件结构以及 jdk的版本
3.选择项目所需要的依赖
然后点击 Next ..最后 finish
4.看一下文件的结构:
5.编辑一下 pom.xml:
4.0.0
com.example
springboot-mybatis-demo
0.0.1-SNAPSHOT
jar
springboot-mybatis-demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-thymeleaf
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-test
test
com.alibaba
druid
1.0.11
com.fasterxml.jackson.core
jackson-core
com.fasterxml.jackson.core
jackson-databind
com.fasterxml.jackson.datatype
jackson-datatype-joda
com.fasterxml.jackson.module
jackson-module-parameter-names
com.github.pagehelper
pagehelper-spring-boot-starter
1.1.2
com.alibaba
druid-spring-boot-starter
1.1.0
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
6.创建数据库
-- 第 1步创建数据库
CREATE DATABASE example_springboot;
-- 第 2步创建表
CREATE TABLE IF NOT EXISTS `tb_user`(
`userId` INT UNSIGNED AUTO_INCREMENT,
`userName` VARCHAR(100) NOT NULL,
`password` VARCHAR(100) NOT NULL,
`phone` VARCHAR(255) NOT NULL,
`remark` VARCHAR(500),
PRIMARY KEY ( `userid` )
)ENGINE=INNODB DEFAULT CHARSET=utf8;
7.项目不使用 application.properties文件 而使用更加简洁的 application.yml文件:
将原有的 resource文件夹下的 application.properties文件删除,创建一个新的 application.yml配置文件,
application.yml 文件的内容如下:
server:
port: 8080
spring:
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/example_springboot
username: root
password: root
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.example.springbootmybatisdemo.model
#pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
8.使用mybatis generator 自动生成代码:
9.点击 Run-Edit Configurations
10.编辑 Maven
11.自动生成的文件
package com.example.springbootmybatisdemo.model;
public class User {
private Integer userId;
private String userName;
private String password;
private String phone;
private String remark;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark == null ? null : remark.trim();
}
}
package com.example.springbootmybatisdemo.mapper;
import com.example.springbootmybatisdemo.model.User;
public interface UserMapper {
int deleteByPrimaryKey(Integer userId);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer userId);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
user_id, user_name, password, phone, remark
delete from tb_user
where user_id = #{userId,jdbcType=INTEGER}
insert into tb_user (user_id, user_name, password,
phone, remark)
values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR})
insert into tb_user
user_id,
user_name,
password,
phone,
remark,
#{userId,jdbcType=INTEGER},
#{userName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{remark,jdbcType=VARCHAR},
update tb_user
user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
remark = #{remark,jdbcType=VARCHAR},
where user_id = #{userId,jdbcType=INTEGER}
update tb_user
set user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
remark = #{remark,jdbcType=VARCHAR}
where user_id = #{userId,jdbcType=INTEGER}
到这一步,就可以快速生成 bean与常用的 dao方法
12.打开类 SpringbootMybatisDemoApplication.java,这个是 springboot的启动类。我们需要添加点东西:
package com.example.springbootmybatisdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.springbootmybatisdemo.mapper")//将项目中对应的 mapper类的路径加进来就可以了
@SpringBootApplication
public class SpringbootMybatisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisDemoApplication.class, args);
}
}
到这里所有的搭建工作都完成了,
接下来就可以写 Service 跟 Controller 了
...... loading