一、步骤
- 导入依赖:MySQL驱动、Druid依赖、MyBatis与Spring Boot整合依赖、Lombok依赖
- 在Service接口实现类上添加@Service注解
- 在Dao接口上添加@Mapper注解
- 在pom中将dao目录注册为资源目录
- 在配置文件中注册映射文件、实体类别名,及数据源
二、需求
完成一个简单注册功能
三、定义工程
四、导入依赖
修改pom.xml配置,导入三个依赖:mybatis与Spring Boot整合依赖、mysql驱动依赖与Druid数据源依赖。
- <?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.6.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.sping.boot.demo</groupId>
- <artifactId>01-primary</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>01-primary</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
- <dependencies>
- <!--mybatis与spring boot整合依赖-->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.2</version>
- </dependency>
-
- <!--mysql驱动-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.47</version>
- </dependency>
-
- <!-- druid驱动 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.12</version>
- </dependency>
-
- <!--tomcat内置的JSP解析器-->
- <dependency>
- <groupId>org.apache.tomcat.embed</groupId>
- <artifactId>tomcat-embed-jasper</artifactId>
- </dependency>
-
- <!--lombok-->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
-
- <build>
- <resources>
- <!--注册Dao包目录下Mybatis映射文件资源目录-->
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- </resource>
-
- <!--注册webapp目录为资源目录-->
- <resource>
- <directory>src/main/webapp</directory>
- <targetPath>META-INF/resources</targetPath>
- <includes>
- <include>**/*.*</include>
- </includes>
- </resource>
- </resources>
-
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
五、创建控制层
五、定义Service接口及实现类
六、定义实体类及DB表
七、定义DB表
八、定义Dao接口
Dao接口上要添加@Mapper注解
九、定义映射文件
十、修改主配置文件
在主配置文件中主要完成以下几件工作
- 注册映射文件
- 注册实体类别名
- 注册数据源
- # 视图的前辍与后辍
- spring:
- mvc:
- view:
- prefix: /
- suffix: .jsp
-
- # 注册数据源
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: com.mysql.jdbc.Driver
- url: jdbc:mysql:///test?useUnicode=true&characterEncoding=utf-8
- username: root
- password: root
-
- mybatis:
- # 注册映射文件
- mapper-locations: classpath:com/abc/dao/*.xml
- # 注册实体类别名
- type-aliases-package: com.abc.bean
十一、创建index.jsp
十二、创建welcome.jsp
jsp/welcome.jsp目录