创建Spring Boot项目
1、New Project
2)取名为sb2 (springboot2.x)
3、勾选Spring Web
4、勾选MyBatis framework
5、下一步,然后点击完成
6、pom.xml增加jar和插件
jar
dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency>
插件
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency> </dependencies> </plugin>
7、增加resources/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> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" userId="root" password="123456"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- targetProject:生成PO类的位置 --> <javaModelGenerator targetPackage="com.example.demo.bean" targetProject="src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 指定数据库表 --> <table schema="" tableName="test"></table> </context> </generatorConfiguration>
8、双击mybatis-generator:generate
9、application.properties
server.port=8080 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.demo.bean mybatis.configuration.map-underscore-to-camel-case=true
10、配置扫码范围
@SpringBootApplication @MapperScan("com.example.demo.mapper") public class Sb2Application { public static void main(String[] args) { SpringApplication.run(Sb2Application.class, args); } }
11、创建Service层
@Component public class DemoService { @Autowired private TestMapper testMapper; public Test getTestInfoById(Integer id){ return Optional.ofNullable(testMapper.selectByPrimaryKey(id)).orElse(null); } }
12、创建Controller层
@Controller @RequestMapping("/demo") public class DemoController { @Autowired private DemoService demoService; @RequestMapping("/hello/{id}") @ResponseBody public String hello(@PathVariable(value = "id") Integer id){ return Optional.ofNullable(demoService.getTestInfoById(id)).map(Test::toString).orElse("empty String"); } }
其中咋Test增加toString方法
public class Test { private Integer id; 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(); } @Override public String toString() { return "Test{" "id=" id ", name=‘" name ‘‘‘ ‘}‘; } }
13、运行
14、MySql数据库名为test
表test的表结构:
表数据