以下主要是完成mybatis的逆向工程
命名各种不规范敬请谅解
创建Spring boot
右下角选择auto-import自动导入,这样你import一个本地maven库中不存在的包的时候他会自动帮你下载
然后pow.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 http://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.1.3.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-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.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.15</version><!--这里注意-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--加载目录下所有mybatis的mapper文件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
</project>
翻开目录,resources里面application.properties直接删掉,重新建个application.yml,两者作用一样,格式不同,而我喜欢优美的格式。mysql建好test数据库,然后driver的话中间要cj是mysql8的驱动
#application.yml
server:
port: 8080
spring:
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
username: root
password: #你的密码
driver-class-name: com.mysql.cj.jdbc.Driver
#这里的mybatis下面要用到,现在不用
mybatis:
mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.example.wechatserver0316.entity # 注意:对应实体类的路径,改成自己的
这时候点右上角启动,看下面状态正常,然后访问,可以看到以下界面,因为没有写对应页面的响应内容,所以error。
mybatis相关
test数据库下建个表,表名demo
这时候application.yml的mybatis配置派上用场了,目的是加载那个目录下的所有mapper的xml文件,之前卡了好长时间就是这个问题!!!
还有就是配置文件里的mybatis generator 自动生成代码插件,用来逆向工程的。
在resource文件夹下创建generator文件夹,并在文件夹中创建generatorConfig.xml文件
这里注意的点就是
- 驱动包的加载
- 数据库连接
- mapper和实体类生成路径
- 要生成的表及映射名
<?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>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包,这里直接找到包拉到项目中,用相对路径就好了,下面讲怎么拉-->
<classPathEntry location="src/mysql-connector-java-8.0.15.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false"/>
</commentGenerator>
<!--数据库连接驱动类,URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8" userId="root" password="gg">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--注意下列com.example.wechatserver0316.mapper中的wechatserver0316必须改为自己的项目名!!!!!!!!!!!!!!!-->
<!-- 生成(实体)模型的包名和位置-->
<javaModelGenerator targetPackage="com.example.wechatserver0316.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成XML映射文件的包名和位置-->
<sqlMapGenerator targetPackage="resources.mapper" targetProject="src/main/">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO接口的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.wechatserver0316.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名,这里类名加个1只是为了测试,统一的话肯定比较好-->
<table tableName="demo" domainObjectName="demo1" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
怎么找到driver?点扩展库,然后输入mysql,会自动搜索到
右键然后打开,再把包复制到项目的src目录下,要和上面的配置文件的路径对应!!!
配置一波
然后运行,可以看到创建了目录和文件!如下三个
xml 这里demo1Mapper生成了很多sql,这里测试就删掉自己写一条(重新生成mapper的时候会增量更新,之前写的sql也不会被替换掉,但是demo1Mapper Interface的就会直接覆盖不见了)
注意select表明了绑定的方法名和参数和返回值类型
<?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">
<mapper namespace="com.example.wechatserver0316.mapper.demo1Mapper">
<select id="getAllByName" parameterType="String" resultType="demo1">
SELECT pwd from demo where name = #{username}
</select>
</mapper>
Dao层 注意加上这两个注解
package com.example.wechatserver0316.mapper;
import com.example.wechatserver0316.entity.demo1;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
@Mapper
@Component
public interface demo1Mapper {
demo1 getAllByName(String username);
}
}
Servicr层 再创建个Services文件夹和类实现上面的接口,注意注解
//demoService.java
package com.example.wechatserver0316.Services;
import com.example.wechatserver0316.entity.demo1;
import com.example.wechatserver0316.mapper.demo1Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class demoService {
@Autowired
private demo1Mapper dM;
public String getPwd(String username) {
demo1 dd = dM.getAllByName(username);
return dd.getPwd();
}
}
Controller层
//DemoController.java
package com.example.wechatserver0316.Controller;
import com.example.wechatserver0316.Services.demoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class DemoController {
@Autowired
demoService ds;
@RequestMapping("/get/{id}")
public String hello(@PathVariable String id){
String s = ds.getAllByName(id);
return s;
}
}
运行,访问,效果如图,完成!
截取一下最终目录吧