• 创建springboot 标准工程(MyBatis)并且打包运行


    实现目标:

    创建一个简单的数据库demo,表名mach,id为自增,mach_no为字符串

     创建Maven的jar工程,pom.xml内容如下

    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.legion</groupId>
        <artifactId>standard</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.2.RELEASE</version>
        </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>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.6</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.28</version>
            </dependency>
        </dependencies>
    </project>

    创建常规的package

    com.legion.controller

    com.legion.dao

    com.legion.pojo

    com.legion.service

    创建包com.legion,新建类MyBootDemo,其中有main函数

    @SpringBootApplication
    public class MyBootDemo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            SpringApplication.run(MyBootDemo.class,args);
        }
    
    }

    注意:其他包必须和springboot入口的package同级,或者处于下一级。

    看到了有的工程在此处使用了

    @ComponentScan(basePackages="XX.YY")
    @MapperScan(basePackages="XX.YY.ZZ")

    在src/main/resources建立application.properties文件

    #下面这一行指定Mapper xml文件位置,发现不配置也可以
    mybatis.mapper-locations=classpath:com/legion/dao/*.xml spring.datasource.url=jdbc:mysql://localhost:3306/demo spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456

    使用Druid的参考配置

    #durid
    spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
    spring.datasource.druid.driver-class-name = com.mysql.jdbc.Driver
    spring.datasource.druid.url = jdbc:mysql://localhost:3306/springboot_mybatis
    spring.datasource.druid.username=root
    spring.datasource.druid.password=root
     
    # 下面为连接池的补充设置,应用到上面所有数据源中
    # 初始化大小,最小,最大
    spring.datasource.druid.initialSize=5
    spring.datasource.druid.minIdle=5
    spring.datasource.druid.maxActive=20
    # 配置获取连接等待超时的时间
    spring.datasource.druid.maxWait=60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 
    spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒 
    spring.datasource.druid.minEvictableIdleTimeMillis=300000
    spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
    spring.datasource.druid.testWhileIdle=true
    spring.datasource.druid.testOnBorrow=false
    spring.datasource.druid.testOnReturn=false
    # 打开PSCache,并且指定每个连接上PSCache的大小 
    spring.datasource.druid.poolPreparedStatements=true
    spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 
    spring.datasource.druid.filters=stat,wall,log4j
    # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    # 合并多个DruidDataSource的监控数据
    spring.datasource.druid.useGlobalDataSourceStat=true

    使用yml的一个参考配置

    server:
      port: 8080
    
    spring:
        datasource:
            name: test
            url: jdbc:mysql://127.0.0.1:3306/depot
            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

    下面的内容和标准的SSM区别不大,注意Mapper接口加上@Mapper注解。

    com.leigon.controller中UserController.java的内容

    @RestController
    public class UserController {
        @Autowired
        private MachineService ms;
        @RequestMapping("/all")
        public List<Machine> findAll(){
            return ms.findAll();
        }
        
        @RequestMapping("/hello")
        public String show() {
            return "hello,world";
        }
        
    }

    com.legion.dao

    MachineMapper.java

    @Mapper
    public interface MachineMapper {
        public List<Machine> findAll();
    }

    MachineMapper.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.legion.dao.MachineMapper">
        <select id="findAll" resultType="com.legion.pojo.Machine">
            select * from mach
        </select>
    
    </mapper>

    com.legion.pojo中的Machine.java

    public class Machine {
        private int id;
        private String mach_no;
    //getter and setter
    ...
    }

    com.legion.service接口和实现

    public interface MachineService {
        public List<Machine> findAll();
    }
    
    
    @Service
    public class MachineServiceImpl implements MachineService {
        
        @Autowired
        private MachineMapper machine;
        @Override
        public List<Machine> findAll() {
            return machine.findAll();
        }
    
    }

     打包

    pom.xml添加

        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

    然后右键项目,run as=-Maven Install,target目录中生成相应的jar文件。

    方法2:run as -Maven Build填写package

    如果出现No compiler is provided in this environment 的错误,

    保证这里是JDK中的JRE

    还有这里勾选上

    额外阅读   ,阅读2 ,,阅读3

    在CentOS上部署运行

    https://blog.csdn.net/lhc0512/article/details/80635929

  • 相关阅读:
    response.setContentType()的作用及参数
    MyEclipse的一些设置
    web项目更改文件后缀,隐藏编程语言
    IntelliJ IDEA 部署远程服务
    java操作cookies
    Java 处理图片 base64 编码的相互转换
    递归与非递归二分查找实现
    js实现url链接encode加密
    mysql 中 SQL_CALC_FOUND_ROWS 功能
    Java 微信登录授权后获取微信用户信息昵称乱码问题解决
  • 原文地址:https://www.cnblogs.com/legion/p/9948851.html
Copyright © 2020-2023  润新知