创建项目完成导入以下依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</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>2.1.3</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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!--引入mybatis generator--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.22</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>maven-lombok-plugin</artifactId> <version>0.9.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
编写application.yml
spring: jackson: date-format: yyyy-MM-dd time-zone: GMT+8 datasource: username: root password: root url: jdbc:mysql://localhost:3306/seer?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver #自定义数据源 type: com.alibaba.druid.pool.DruidDataSource #Spring Boot 默认是不注入这些属性值的,需要自己绑定 #druid 数据源专有配置 #初始化建立的物理连接个数 initialSize: 5 #最小连接池数量 minIdle: 5 #最大连接池数量 maxActive: 20 #获取连接时最大等待时间,单位ms maxWait: 60000 #有两个含义: 1) Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableldleTimeMillis则关闭物理连接 #2)testWhileldle的判断依据,详细看testWhileldle属性的说明 timeBetweenEvictionRunsMillis: 60000 #连接保持空闲而不被驱逐的最大时间 minEvictableIdleTimeMillis: 300000 #用来检测连接是否有效的饿sql validationQuery: SELECT 1 FROM DUAL #建议配置为true ,不影响性能,并且保证安全性。申请连接的时候 #检测,如果空闲时间大于timeBetweenEvictionRunsMillis ,执行 #validationQuery检测连接是否有效。默认false testWhileIdle: true #申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。默认true testOnBorrow: false #归还连接时执行validationQueny检测连接是否有效,做了这个配置会降低性能。默认true testOnReturn: false #是否缓存preparedStatement ,也就是PSCache。PSCache对支持 #游标的数据库性能提升巨大,比如说oracle.在mysq|下建议关闭。默认false poolPreparedStatements: true #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入 #如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
启动测试完成
添加MyBatis Generator
编写mbg.xml在根目录
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="dsql" targetRuntime="MyBatis3Simple"> <!--指定如何连接数据库-->改写自己的数据库账号密码 <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/seer?useSSL=false&serverTimezone=UTC" userId="root" password="root"/> <!--指定javabean生成策略-->自己的实体类地址 <javaModelGenerator targetPackage="com.example.demo.pojo" targetProject="src/main/java"> <property name="enableSubPackages" value="ture"/> <property name="trimStrings" value="ture"/> </javaModelGenerator> <!--sql映射生成策略--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="ture"/> </sqlMapGenerator> <!--指定mapper接口所在位置-->自己的dao层位置 <javaClientGenerator targetPackage="com.example.demo.dao" targetProject="src/main/java" type="XMLMAPPER"/> //修改为自己的表名 <table tableName="spirit" domainObjectName="Spirit"/> <!-- <table tableName="skill" domainObjectName="Skill"/>--> <!-- <table tableName="attribute" domainObjectName="Attribute"/>--> <!-- <table tableName="ability" domainObjectName="Ability"/>--> <!-- <table tableName="engrave" domainObjectName="Engrave"/>--> <!-- <table tableName="spirit_ability" domainObjectName="Spirit_ability"/>--> <!-- <table tableName="spirit_engrave" domainObjectName="Spirit_engrave"/>--> </context> </generatorConfiguration>
修改application.yml
mybatis: #设置别名 type-aliases-package: com.example.demo.pojo #配置 *.xml文件 mapper-locations: classpath:mapper/*.xml server: #端口号 port: 8080
编写Testmbg
public class Testmbg { @Test public void mbgTest() throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("mbg.xml"); 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); } }
执行完成后生成;
添加freemarker配置
freemarker: settings: classic_compatible: true #处理空值 suffix: .ftl template-loader-path: - classpath:/templates mvc: static-path-pattern: /static/**
编写test.ftl
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>test</h1> </body> </html>
在mapper中添加注解
在SpiritServiceImpl中添加注解