org.mybatis.spring.boot
mybatis-spring-boot-starter
mysql
mysql-connector-java
5.1.21
com.alibaba
druid
1.0.27
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.7
org.mybatis.generator
mybatis-generator-maven-plugin
${mybatis.generator.version}
mysql
mysql-connector-java
${mysql.version}
org.mybatis.generator
mybatis-generator-core
${mybatis.generator.version}
true
true
src/main/resources/mybatis/mybatis-generator.xml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/dolphin?useUnicode=true&characterEncoding=UTF-8&tinyInt1isBit=false
username: liuzhiqiang
password: lzq199528
driver-class-name: com.mysql.jdbc.Driver
# DruidDataSource 阿里数据库连接池
type: com.alibaba.druid.pool.DruidDataSource
#连接池的配置信息
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
useGlobalDataSourceStat: true
#mybatis 配置信息
mybatis:
type-aliases-package: com.dolphin.domain
config-location: classpath:/mybatis/mybatis-config.xml
mapperLocations: classpath:/mappers/**/*.xml
CREATE TABLE `NewTable` (
`id` bigint(16) NOT NULL AUTO_INCREMENT ,
`user_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名' ,
`password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '密码' ,
`user_state` tinyint(2) NOT NULL DEFAULT 0 COMMENT '用户状态 0 可登陆 1不可登录' ,
`phone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '联系方式(手机号)' ,
`real_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '真实姓名' ,
`user_img` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户头像' ,
`create_by` bigint(16) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建者' ,
`create_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间' ,
`update_by` bigint(16) UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新者' ,
`update_date` datetime NULL DEFAULT NULL COMMENT '更新时间' ,
`del_flag` tinyint(2) NOT NULL DEFAULT 0 COMMENT '删除标示 0正常 1删除 ' ,
`remarks` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '备注' ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_general_ci
AUTO_INCREMENT=1
ROW_FORMAT=DYNAMIC
;
7.使用maven里的plugins 来生成 文件
如果不出错的话,此时我们就能在想对应的目录下看到我们生成的mapper,sql,model文件了
编写测试controller MybatisController.java
package com.dolphin.controller;
import com.dolphin.domain.sys.AdminUser;
import com.dolphin.mapper.sys.AdminUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* 开发公司:青岛海豚数据技术有限公司
* 版权:青岛海豚数据技术有限公司
*
* MybatisController
*
* @author 刘志强
* @created Create Time: 2019/2/15
*/
@RestController
@RequestMapping("/mybatis")
public class MybatisController {
@Autowired
public AdminUserMapper adminUserMapper;
/**
* insert
* @return
*/
@GetMapping("add")
@ResponseBody
public String add(){
AdminUser adminUser = new AdminUser();
adminUser.setUserName("刘志强");
adminUser.setRealName("刘志强");
adminUser.setPassword("123456");
int i = adminUserMapper.insertSelective(adminUser);
if (i > 0) {
return "添加成功";
} else {
return "添加失败";
}
}
/**
* update
* @return
*/
@GetMapping("up")
@ResponseBody
public String up(){
AdminUser adminUser = new AdminUser();
adminUser.setId(new Long("1"));
adminUser.setUserName("刘志强");
adminUser.setRealName("王妍");
adminUser.setPassword("123456");
int i = adminUserMapper.updateByPrimaryKeySelective(adminUser);
if (i > 0) {
return "修改成功";
} else {
return "修改失败";
}
}
/**
* select
* @return
*/
@GetMapping("get")
@ResponseBody
public AdminUser get(Long id) {
AdminUser adminUser = adminUserMapper.selectByPrimaryKey(id);
return adminUser;
}
}
访问接口观察
Druid 配置
新建Druid 配置 DruidConfig.java
package com.dolphin.config.druid;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* 开发公司:青岛海豚数据技术有限公司
* 版权:青岛海豚数据技术有限公司
*
* DruidConfig
*
* @author 刘志强
* @created Create Time: 2019/2/15
*/
@Configuration
@ServletComponentScan //用于扫描所有的Servlet、filter、listener
public class DruidConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}
}
package com.dolphin.config.druid;
import com.alibaba.druid.support.http.WebStatFilter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
/**
* 开发公司:青岛海豚数据技术有限公司
* 版权:青岛海豚数据技术有限公司
*
* DruidStatFilter
*
* @author 刘志强
* @created Create Time: 2019/2/15
*/
@WebFilter(
filterName = "druidWebStatFilter",
urlPatterns = {"/*"},
initParams = {
@WebInitParam(name = "exclusions", value = "*.js,*.jpg,*.png,*.gif,*.ico,*.css,/druid/*")//配置本过滤器放行的请求后缀
}
)
public class DruidStatFilter extends WebStatFilter {
}
package com.dolphin.config.druid;
import com.alibaba.druid.support.http.StatViewServlet;
import javax.servlet.Servlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
* 开发公司:青岛海豚数据技术有限公司
* 版权:青岛海豚数据技术有限公司
*
* DruidStatViewServlet
*
* @author 刘志强
* @created Create Time: 2019/2/15
*/
@WebServlet(
urlPatterns = {"/druid/*"},
initParams = {
@WebInitParam(name = "allow", value = "127.0.0.1"),
@WebInitParam(name = "loginUsername", value = "root"),
@WebInitParam(name = "loginPassword", value = "123"),
@WebInitParam(name = "resetEnable", value = "true")// 允许HTML页面上的“Reset All”功能
}
)
public class DruidStatViewServlet extends StatViewServlet implements Servlet {
private static final long serialVersionUID = 1L;
}