写的超级超级简单明了哦!!!!!
创建一个maven工程的过程就省略啦~,springboot、数据库的相关依赖正常引用就行~
- pom.xml文件引入mybatis依赖和generator插件。
mybatis的依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
mybatis-generator插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--generator插件配置文件的位置--> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
完整版pom.xml文件
<?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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yxw</groupId>
<artifactId>ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ssm</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>
<!--mybatis的依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>5.1.45</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>
</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>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2.在resources文件下建立generatorConfig.xml文件,主要修改数据库的连接信息、mybatis的xml文件、mapper文件以及反射类文件的地址信息。
以下有 6处 需要根据自己的来修改,完整版如下。
<?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>
<!-- 需要修改1 mysql 连接数据库jar 这里选择自己本地位置-->
<classPathEntry location="/Users/yxw/app/maven/repository/mysql/mysql-connector-java/5.1.45/mysql-connector-java-5.1.45.jar" />
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 需要修改2 数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm" userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 需要修改3:targetPackage 生成体类类的位置 -->
<javaModelGenerator targetPackage="com.yxw.ssm.entity"
targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 需要修改4: targetPackage 生成xml的位置,一般都在resources目录下 -->
<sqlMapGenerator targetPackage="mybatis.mapper"
targetProject="src/main/resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 需要修改5 targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.yxw.ssm.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 需要修改6 指定数据库表 -->
<table tableName="bm_acct_loan" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
工程目录结构
3.在工程右边找到maven中插件中的generator,点击运行实体类以及xml文件就自动生成啦!!!
4.修改应用的启动类,扫包注册bean。用@ComponentScan扫所有包。@MapperScan扫mapper接口的包
@SpringBootApplication
@ComponentScan(basePackages = {"com.yxw.ssm.controller","com.yxw.ssm.dao","com.yxw.ssm.entity","com.yxw.ssm.service"})
@MapperScan("com.yxw.ssm.dao")
public class SsmApplication {
public static void main(String[] args) {
SpringApplication.run(SsmApplication.class, args);
}
}
5.配置文件application.yml,需要配置数据库信息以及mybatis信息
server:
port: 9015
spring:
profiles: dev
datasource:
url: jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
mybatis:
mapper-locations: classpath*:mybatis/mapper/*.xml # ------xml的路径
type-aliases-package: om.yxw.ssm.entity # ------实体类的路径
到这里我们工程基本差不多了,我们就可以写相关的service层和controller层啦~
6.编写service接口以及它的实现类,直接贴实现类的代码啦!(记得自动生成的mapper的接口中并没有自动注入,要使用@Repository在其类上进行注入)
@Service("bmAcctLoanServiceImpl")
public class BmAcctLoanServiceImpl implements BmAcctLoanService {
@Resource
BmAcctLoanMapper bmAcctLoanMapper;
@Override
public BmAcctLoan selectByPrimaryKey(Integer id) {
return bmAcctLoanMapper.selectByPrimaryKey(id);
}
}
7.controller层
@Controller
@RequestMapping("/cms")
public class BmAcctLoanController {
@Autowired
BmAcctLoanService bmAcctLoanService;
@RequestMapping("/find")
@ResponseBody
public Object selectByPrimaryKey(int id ){
return bmAcctLoanService.selectByPrimaryKey(id);
}
}
最后调用http://localhost:9015/cms/find/?id=1就可了啦