Springboot 2.0.4+ beetlsql 整合
时间:2019-01-16
本文章向大家介绍Springboot 2.0.4+ beetlsql 整合,主要包括Springboot 2.0.4+ beetlsql 整合使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
pom配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!--beetlsql-->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-framework-starter</artifactId>
<version>1.1.35.RELEASE</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--junit测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version> 4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
附上结构图
tb_department
CREATE TABLE `tb_department` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`creation_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4;
tb_employee
CREATE TABLE `tb_employee` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`hire_time` datetime DEFAULT NULL,
`department_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4;
application.properties
server.port=80
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#beetlsql
//DAO包路径,否则无法注入
beetlsql.basePackage=com.terry
beetlsql.daoSuffix=DAO
//(源码的注释)下划线命名转换,数据库 Sys_User,对应类SysUser,列user_Id,对应属性userId
beetlsql.nameConversion=org.beetl.sql.core.UnderlinedNameConversion
beetlsql.sqlPath=/sql
先从department开始把,毕竟没有部门,哪里来的员工
BaseModel
@Getter@Setter
public class BaseModel implements Serializable {
//如果是自增主键,并且也叫id,则AutoID可以省略,更多请参考beetl官网
@AutoID
private Long id;
}
Department
@Getter
@Setter
@Table(name="tb_department")
public class Department extends BaseModel {
private String title;
private Date creationTime;
}
DepartmentDAO,这里直接继承BaseMapper就可以了
public interface DepartmentDAO extends BaseMapper<Department> {
}
DepartmentService
@Service
public class DepartmentService {
@Autowired
private DepartmentDAO departmentDAO;
public Department add(Department department){
departmentDAO.insertTemplate(department,true);
return departmentDAO.unique(department.getId());
}
public List<Department> listAll(){
return departmentDAO.all();
}
public Department selectById(Long id){
return departmentDAO.unique(id);
}
}
DepartmentController
@RestController
@RequestMapping("/dep")
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
@PostMapping("/add")
@ResponseBody
public String add(Department department){
department.setCreationTime(new Date());
Department de = departmentService.add(department);
return JSON.toJSONString(de);
}
@GetMapping("list")
public String listAll(){
List<Department> departments = departmentService.listAll();
return JSON.toJSONString(departments);
}
}
接下来是Employee部分
@Getter@Setter
@Table(name="tb_employee")
public class Employee extends BaseModel{
private String name;
private Date hireTime;
private Long departmentId;
private Department department;
}
EmployeeDAO
public interface EmployeeDAO extends BaseMapper<Employee> {
//这里自定义了一个方法
@SqlStatement(params = "departmentId",returnType = Employee.class)
List<Employee> selectByDepartmentId(Long departmentId);
}
EmployeeService
@Service
public class EmployeeService {
@Autowired
private EmployeeDAO employeeDAO;
@Autowired
private DepartmentDAO departmentDAO;
public Employee add(Employee employee){
employeeDAO.insertTemplate(employee,true);
return employeeDAO.unique(employee.getId());
}
public List<Employee> listAll(){
List<Employee> list = employeeDAO.all();
list.forEach(m->m.setDepartment(departmentDAO.unique(m.getDepartmentId())));
return list;
}
public List<Employee> selectByDepartmentId(Long departmentId) {
List<Employee> list = employeeDAO.selectByDepartmentId(departmentId);
list.forEach(m->m.setDepartment(departmentDAO.unique(m.getDepartmentId())));
return list;
}
public int update(Employee employee) {
return employeeDAO.updateTemplateById(employee);
}
}
EmployeeController
@RestController
@RequestMapping("/emp")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@PostMapping("/add")
public String add(Employee employee){
employee.setHireTime(new Date());
Employee emp = employeeService.add(employee);
return JSON.toJSONString(emp);
}
@GetMapping("/all")
public String all(){
return JSON.toJSONString(employeeService.listAll());
}
@GetMapping("/getByDepId")
public String get(Long departmentId){
return JSON.toJSONString(employeeService.selectByDepartmentId(departmentId));
}
@PostMapping("/update")
public String update(Employee employee){
int i = employeeService.update(employee);
if (i==0){
return "更新失败";
}else return "更新成功";
}
}
employee.md
selectByDepartmentId
===
select * from tb_employee where 1=1
@if(isNotEmpty(departmentId)){
and department_id = #departmentId#
@}
启动类
@SpringBootApplication
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class,args);
}
}
大功告成啦~
- Python Windows下分布式进程的坑(分布式进程的一个简单例子)
- Python pdb调试
- linux学习第三十五篇:LAMP架构介绍,MySQL_MariaDB介绍,MySQL安装
- 尝试克服一下小伙伴对神经网络的恐惧No.26
- Python 元类(MetaClass)
- linux学习第三十六篇:MariaDB安装,Apache安装
- Python 5种连接字符串的方法
- Python 装饰器(Decorator)
- linux学习第三十七篇:安装PHP5,安装PHP7
- linux学习第三十八篇: Apache和PHP结合,Apache默认虚拟主机
- Python lambda表达式
- Python的闭包(Closure)与惰性计算(Lazy Evaluation)
- linux学习第三十九篇:Apache用户认证,域名跳转,Apache访问日志
- Python sorted()函数
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解