SpingBoot与BeetlSQL结合

1 必要安装

jdk1.8、maven、idea、mysql。

2 在idea中新建maven项目。添加相关的依赖。依赖如下:



    4.0.0

    cn.ancony
    springboot0501
    1.0-SNAPSHOT
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.0.RELEASE
    
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-devtools
            true
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            com.ibeetl
            beetl-framework-starter
            1.1.40.RELEASE

        
        
            com.zaxxer
            HikariCP
        
        
            mysql
            mysql-connector-java
            6.0.5
        

    

    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

3 resource根目录下面建立application.properties文件。内容如下:

#mysql数据库连接配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/orm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#beetelsql的配置
beetlsql.basePackage=cn.ancony.springboot0501.dao
beetl-beetlsql.dev=true

#热部署的配置
spring.devtools.restart.enabled=true

4 编写启动的应用程序。

package cn.ancony.springboot0501;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class,args);
    }
}

5 编写实体类User,对应数据库的User表。

package cn.ancony.springboot0501.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
    private Integer id;
    private Integer departmentId;
    private String name;
    private Date createTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Integer departmentId) {
        this.departmentId = departmentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

6 在mysql中建立数据库orm,将相关数据导入mysql。导入的文件为db.sql。文本的内容如下:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) COLLATE utf8_bin DEFAULT NULL COMMENT '名称',
  `department_id` int(11) DEFAULT NULL,
  `create_time` date DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE `department` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;


INSERT INTO `user` VALUES (1, 'helo', 1, '2017-4-21 11:52:41');
INSERT INTO `department` VALUES ('1', '研发部门');

7 创建userDao接口。

package cn.ancony.springboot0501.dao;

import cn.ancony.springboot0501.entity.User;
import org.beetl.sql.core.annotatoin.SqlResource;
import org.beetl.sql.core.mapper.BaseMapper;

import java.util.List;

@SqlResource("www.user")
public interface UserDao extends BaseMapper {
    List selectSample(User query);
}

8 在resources文件夹下面创建www文件夹,在www文件夹下面创建user.md文件,文件内容如下:

selectSample
===

* 一个简单的查询例子

	select * from user where 1=1
	

9 创建数据源的配置。

package cn.ancony.springboot0501.config;


import com.zaxxer.hikari.HikariDataSource;
import org.beetl.sql.ext.spring4.BeetlSqlDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {
    @Bean(name = "dataSource")
    public DataSource dataSource(Environment env) {
        HikariDataSource ds = new HikariDataSource();
        ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
        ds.setUsername(env.getProperty("spring.datasource.username"));
        ds.setPassword(env.getProperty("spring.datasource.password"));
        ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        return ds;
    }

    @Bean
    public BeetlSqlDataSource beetlSqlDataSource(@Qualifier("dataSource") DataSource dataSource) {
        BeetlSqlDataSource source = new BeetlSqlDataSource();
        source.setMasterSource(dataSource);
        return source;
    }
}

10 创建提供服务的接口。

package cn.ancony.springboot0501.service;

import cn.ancony.springboot0501.entity.User;

import java.util.List;

public interface UserService {
    User getUserById(Integer id);

    List select(String name);

}

11 创建服务接口的实现类

package cn.ancony.springboot0501.service.impl;

import cn.ancony.springboot0501.dao.UserDao;
import cn.ancony.springboot0501.entity.User;
import cn.ancony.springboot0501.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    @Override
    public User getUserById(Integer id) {
        return userDao.unique(id);
    }

    @Override
    public List select(String name) {
        User paras = new User();
        paras.setName(name);
        return userDao.selectSample(paras);
    }
}

12 创建controller类。

package cn.ancony.springboot0501.controller;

import cn.ancony.springboot0501.entity.User;
import cn.ancony.springboot0501.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/user/{id}")
    public @ResponseBody
    User say(@PathVariable Integer id) {
        User user = userService.getUserById(id);
        return user;
    }

    @RequestMapping("/user/query/{name}")
    public @ResponseBody
    List say(@PathVariable String name) {
        List users = userService.select(name);
        return users;
    }
}

13 测试。

启动应用程序。因为数据库里面只有一条数据,所以访问http://localhost:8080/user/1。

访问结果:

{"id":1,"departmentId":1,"name":"helo","createTime":"2017-04-20T16:00:00.000+0000"}

访问http://localhost:8080/user/query/helo。

访问结果:

[{"id":1,"departmentId":1,"name":"helo","createTime":"2017-04-20T16:00:00.000+0000"}]

 

你可能感兴趣的:(Java框架)