美文网首页
Springboot 2.0 .4+ beetlsql 整合

Springboot 2.0 .4+ beetlsql 整合

作者: YUTerry | 来源:发表于2018-12-31 11:27 被阅读0次

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

    大功告成啦~

    相关文章

      网友评论

          本文标题:Springboot 2.0 .4+ beetlsql 整合

          本文链接:https://www.haomeiwen.com/subject/kreilqtx.html