springboot+mybatis+jpa全过程 基础配置 增加mybatis 增加JPA 增加热部署支持 修改druid配置为druid-spring-boot-starter

springboot+mybatis+jpa全过程
基础配置
增加mybatis
增加JPA
增加热部署支持
修改druid配置为druid-spring-boot-starter

使用springboot、mybatis和jpa搭建项目。

新建项目

springboot+mybatis+jpa全过程
基础配置
增加mybatis
增加JPA
增加热部署支持
修改druid配置为druid-spring-boot-starter
springboot+mybatis+jpa全过程
基础配置
增加mybatis
增加JPA
增加热部署支持
修改druid配置为druid-spring-boot-starter
springboot+mybatis+jpa全过程
基础配置
增加mybatis
增加JPA
增加热部署支持
修改druid配置为druid-spring-boot-starter
springboot+mybatis+jpa全过程
基础配置
增加mybatis
增加JPA
增加热部署支持
修改druid配置为druid-spring-boot-starter

修改pom.xml

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>
                <dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.28</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
				<configuration>
					<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
					<overwrite>true</overwrite>
					<verbose>true</verbose>
				</configuration>
			</plugin>
		</plugins>
	</build>

增加数据库配置

application.properties

# database-config
spring.datasource.url=jdbc:mysql://192.168.6.250/TICP
spring.datasource.username=TICP10X0
spring.datasource.password=TICP10X0
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

# mybatis-config
mybatis.type-aliases-package=cn.com.ncjk.pictransform.common.model
mybatis.mapper-locations=classpath:mapping/*.xml

增加mybatis

增加generatorConfig.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry  location="E:mavenapache-maven-3.3.9
epositorymysqlmysql-connector-java5.1.46mysql-connector-java-5.1.46.jar"/>
    <context >
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://119.23.71.218/test" userId="root" password="cc">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.yihengliu.springbootdemo.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.yihengliu.springbootdemo.mapping" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

springboot+mybatis+jpa全过程
基础配置
增加mybatis
增加JPA
增加热部署支持
修改druid配置为druid-spring-boot-starter

编写java文件

UserController

package com.yihengliu.springbootdemo.controller;

import com.yihengliu.springbootdemo.model.User;
import com.yihengliu.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户controller类
 *
 * @author liucheng
 * @version 0.1
 * @since 0.1 2018-05-06 14:34
 **/
@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/add")
    public int addUser(User user) {
       return userService.addUser(user);
    }
}

UserService

package com.yihengliu.springbootdemo.service;

import com.yihengliu.springbootdemo.model.User;

import java.util.List;

/**
 * 用户servcie接口类
 *
 * @author liucheng
 * @version 0.1
 * @since 0.1 2018-05-06 14:35
 **/
public interface UserService {
    int addUser(User user);
}

UserServiceImpl

package com.yihengliu.springbootdemo.service;

import com.yihengliu.springbootdemo.mapper.UserMapper;
import com.yihengliu.springbootdemo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户service实现类
 *
 * @author liucheng
 * @version 0.1
 * @since 0.1 2018-05-06 14:37
 **/
@Service(value = "userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int addUser(User user) {
        return userMapper.insertSelective(user);
    }
}

SpringBootDemoApplication

package com.yihengliu.springbootdemo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.yihengliu.springbootdemo.mapper")
public class SpringBootDemoApplication {

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

测试

springboot+mybatis+jpa全过程
基础配置
增加mybatis
增加JPA
增加热部署支持
修改druid配置为druid-spring-boot-starter

增加JPA

修改实体类

User

package com.yihengliu.springbootdemo.model;

import javax.persistence.*;

@Entity
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }
}

增加jpa接口类

UserJpa

package com.yihengliu.springbootdemo.jpa;

import com.yihengliu.springbootdemo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import java.io.Serializable;

/**
 * 用户jpa接口类
 *
 * @author liucheng
 * @version 0.1
 * @since 0.1 2018-05-06 16:20
 **/
public interface UserJpa extends Serializable, JpaRepository<User, Integer>, JpaSpecificationExecutor<User> {
}

修改controller类

UserController

package com.yihengliu.springbootdemo.controller;

import com.yihengliu.springbootdemo.jpa.UserJpa;
import com.yihengliu.springbootdemo.mapper.UserMapper;
import com.yihengliu.springbootdemo.model.User;
import com.yihengliu.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 用户controller类
 *
 * @author liucheng
 * @version 0.1
 * @since 0.1 2018-05-06 14:34
 **/
@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/add")
    public int addUser(User user) {
       return userService.addUser(user);
    }

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserJpa userJpa;

    @ResponseBody
    @RequestMapping(value="findAllUser")
    public List<User> findAllUser() {
        return userJpa.findAll();
    }

    @ResponseBody
    @RequestMapping(value="findUser")
    public User findUser(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }
}

测试

springboot+mybatis+jpa全过程
基础配置
增加mybatis
增加JPA
增加热部署支持
修改druid配置为druid-spring-boot-starter

springboot+mybatis+jpa全过程
基础配置
增加mybatis
增加JPA
增加热部署支持
修改druid配置为druid-spring-boot-starter
springboot+mybatis+jpa全过程
基础配置
增加mybatis
增加JPA
增加热部署支持
修改druid配置为druid-spring-boot-starter

增加热部署支持

增加类修改热部署

此方式同样还是只支持在方法内部改变的时候生效

增加依赖包

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<scope>provided</scope>
	<optional>true</optional>
</dependency>	    

生效触发

这里建议使用"ctrl+shif+f9"的方式重新编译,而不是设置自动编译。

thymeleaf修改自动生效

增加maven配置

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

生效触发

这里建议使用"ctrl+shif+f9"的方式重新编译,而不是设置自动编译。

修改druid配置为druid-spring-boot-starter

修改pom.xml配置,修改原druid配置为druid-spring-boot-starter

<dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.1.9</version>
</dependency>

增加druid的filter配置

在配置文件“application.properties”中增加filter配置

spring.datasource.druid.aop-patterns=cn.com.ncjk.pictransform.pic2bigfile.service.*
spring.datasource.druid.filter.stat.db-type=mysql
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000

重启测试

浏览器打开查看druid监控http://localhost:8088/druid
springboot+mybatis+jpa全过程
基础配置
增加mybatis
增加JPA
增加热部署支持
修改druid配置为druid-spring-boot-starter