创建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.0modelVersion>
    <groupId>com.legiongroupId>
    <artifactId>standardartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.2.RELEASEversion>
    parent>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.1.1version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.28version>
        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注解。

创建springboot 标准工程(MyBatis)并且打包运行_第1张图片

 

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

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

com.legion.dao

MachineMapper.java

@Mapper
public interface MachineMapper {
    public List 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 findAll();
}


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

}

 打包

pom.xml添加

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

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

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

 

 

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

保证这里是JDK中的JRE

创建springboot 标准工程(MyBatis)并且打包运行_第2张图片

还有这里勾选上

创建springboot 标准工程(MyBatis)并且打包运行_第3张图片

 

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

 

在CentOS上部署运行

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

 

转载于:https://www.cnblogs.com/legion/p/9948851.html

你可能感兴趣的:(创建springboot 标准工程(MyBatis)并且打包运行)