从零搭建springboot+mybatis逆向工程
一、从零搭建springboot+mybatis逆向工程
1、新建项目
2、next到这里要勾选这两项
第一次有点慢,等一会儿就好
3、在pom.xml中添加mybatis-generator插件
只把图片中的复制到项目中即可
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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.1</version> </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> </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.7</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> <configurationFile> src/main/resources/generatorConfig.xml </configurationFile> </configuration> <dependencies> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
4、在resources创建下创建generatorConfig.xml
注意这里的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://127.0.0.1:3306/db_boot?characterEncoding=utf8" userId="root" password="root"> </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.pojo" targetProject=".\src\main\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.example.mapper" targetProject=".\src\main\resources"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject=".\src\main\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 --> <!-- <table schema="" tableName="tb_item" domainObjectName="Item"></table> domainObjectName 可以配置生成的pojo名字 如不需要example类添加下面四个属性 enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"></table>--> <table schema="db_boot" tableName="t_user"> <!--去掉表结构中的前缀--> <domainObjectRenamingRule searchString="^T" replaceString="" /> </table> </context> </generatorConfiguration>
5、在这之前需要现有数据库和表
6、使用maven的mybatis-generator插件
如果右边找到不到maven可以
7、双击
8、验证
成功生成
二、一个简单的接口
上面逆向工程已经生成,接下来简单是使用。
1、在application.properties中添加基本配置
#配置端口号 server.port=8080 #数据源 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db_boot?characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #配置.xml文件路径 mybatis.mapper-locations=classpath:com.example.mapper/*.xml #配置模型路径 mybatis.type-aliases-package=com.example.pojo #开启debug debug=true #com.mycompany.mavenspringboot.controller 日志 WARN级别输出 logging.level.com.mycompany.mavenspringboot.controller=WARN #com.mycompany.mavenspringboot.mapper sql日志 DEBUG级别输出 logging.level.com.gaohan.stzb.dao=DEBUG #logging.file=./logs/logging.log logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n
2、在pom中引入MySQL驱动和druid数据源
<!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
3、创建service层接口
package com.example.service; import com.example.pojo.User; public interface UserService { User selectByPrimaryKey(Integer id); }
4、创建service实现类
package com.example.service.impl; import com.example.mapper.UserMapper; import com.example.pojo.User; import com.example.service.UserService; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service("userService") public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public User selectByPrimaryKey(Integer id) { return userMapper.selectByPrimaryKey(id); } }
注意:要在UserMaper中加上@Mapper注解,不然无法正确的注入
5、创建Controller层
package com.example.controller; import com.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/") public class UserController { @Autowired private UserService userService; @RequestMapping("index") public String index(){ return userService.selectByPrimaryKey(1).toString(); } }
6、启动DemoApplication
这里有个坑(自己挖的坑自己也跳进去了,哭辽),springboot的启动类必须提高他的目录结构,也就是启动类所在的目录要包含所有的类
7、浏览器验证
为了方便看到效果,为user类添加tostring方法
成功
相关推荐
flydoging 2020-06-07
疯狂紫萧 2020-05-29
yunzhonmghe 2020-05-19
米虚 2020-05-19
flydoging 2020-05-05
WflytoC 2020-05-01
沉浮于世 2020-04-08
yunzhonmghe 2020-04-07
kevincheung 2020-03-28
milidou 2020-02-22
licwwqy 2020-02-18
licwwqy 2020-01-23
milidou 2020-01-11
米虚 2020-01-05
WflytoC 2019-12-19
沉浮于世 2019-12-07
沉浮于世 2019-11-17