YD:springboot整合
1、pom依赖
<!--引入父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- web相关依赖-->
<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>
</dependency>
<!-- Spring Boot提供的配置处理器依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 阿里巴巴的Druid数据源依赖启动器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- MyBatis依赖启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- MySQL数据库连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- mybatis-generator-core 反向生成java代码-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- 引入thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
2、application.properties(springboot主配置文件)
#配置数据源
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/roomdb?serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=30
spring.datasource.druid.max-active=50
spring.datasource.druid.min-idle=20
#开启驼峰写法
mybatis.configuration.map-underscore-to-camel-case=true
#如果配置文件 和 类名对应包名可以省略,否则 需要声明位置
#mybatis.mapper-locations=classpath:mapper/*.xml
#配置mybatis别名
mybatis.type-aliases-package=com.aaa.entity
#mybatis.mapper-locations=classpath:com/aaa/dao/*.xml
#配置jsp
#spring.mvc.view.prefix=/public/jsp/
#spring.mvc.view.suffix=.jsp
#关闭缓存
spring.thymeleaf.cache=false
#编码格式
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
#日志管理配置:一般情况下就是指明文件位置 指明打印级别 打印的格式等等
#logging.level.com.example.demo.mapper=DEBUG
#logging.file.path=log.path
#logging.config=classpath:logback-spring.xml
3、编写启动类
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
4、配置类
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// 前面写访问的路径 后面是资源路径 默认的是 templates+.html
registry.addViewController("/student.html").setViewName("student");
registry.addViewController("/owner.html").setViewName("owner");
registry.addViewController("/ownerinsert.html").setViewName("ownerinsert");
registry.addViewController("/ownerupdate.html").setViewName("ownerupdate");
}
}
5、springboot逆向工程
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>
<!--MYSQL ——整合通用Mapper版本的你想工程-->
<context id="DB2Tables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="javaFileEncoding" value="UTF-8"/>
<!--通用 Mapper 插件,可以生成带注解的实体类-->
<!-- <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.example.demo.common.CommonMapper"/>
<property name="caseSensitive" value="true"/>
<property name="forceAnnotation" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
</plugin>
-->
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
<property name="addRemarkComments" value="true"/>
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/roomdb?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC"
userId="root"
password="root">
<!--没有则只有查询和新增方法-->
<property name="useInformationSchema" value="true"/>
</jdbcConnection>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.aaa.entity" targetProject="src/main/java">
<!-- 是否 自动为每一个生成的类创建一个构造方法-->
<property name="constructorBased" value="false"/>
<property name="useActualColumnNames" value="true"/>
<!-- 在targetPackage的基础上,根据数据库的schema再生成一层package-->
<property name="enableSubPackages" value="false"/>
<!-- 是否创建一个不可变的类 -->
<property name="immutable" value="false"/>
<!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成的映射文件包名和位置 -->
<sqlMapGenerator targetPackage="com.aaa.dao" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.aaa.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="tab_owner" domainObjectName="Owner"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true">
</table>
<table tableName="tab_room" domainObjectName="Room"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true">
</table>
<!-- <table tableName="tab_room" domainObjectName="Room"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<!– mysql 配置–>
<generatedKey column="room_id" sqlStatement="SELECT LAST_INSERT_ID()" identity="true"/>
</table>-->
<!-- <table tableName="tab_customer" domainObjectName="TabCustomer"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<!–mysql 配置–>
<generatedKey column="customerid" sqlStatement="SELECT LAST_INSERT_ID()" identity="true"/>
</table>
-->
</context>
</generatorConfiguration>
逆向工程启动类(需要修改xml路径)MybatisGeneratorUtil
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MybatisGeneratorUtil {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
//如果这里出现空指针,直接写绝对路径即可。
String genCfg = "E:\\IDEA_PROJECT\\vueelementui\\src\\main\\resources\\generatorConfig.xml";
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// 指定配置文件
File configFile = new File(genCfg);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
6、日期格式转化
String转date
@Configuration
public class DateConvert implements Converter<String, Date> {
@Override
public Date convert(String s) {
// 将string ----> Date 类型
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = simpleDateFormat.parse(s);
System.out.println("DateConvert-date:"+date);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
7、启动日志打印sql
logging.level.com.aaa.dao=DEBUG
8、头文件复制
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class Test {
@org.junit.Test
public void test1(){
}
}
mapper头文件
<?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">