ssm整合:Mybatis层


环境要求

IDEA

Mysql 5.7.37

Tomcat 8

Maven 3.6

搭建数据库环境:

1.新建maven项目,命名ssmbuild

2.导入相关pom依赖! 



junit
junit
4.12



mysql
mysql-connector-java
5.1.47



com.mchange
c3p0
0.9.5.2



javax.servlet
servlet-api
2.5


javax.servlet.jsp
jsp-api
2.2


javax.servlet
jstl
1.2




org.mybatis
mybatis
3.5.2


org.mybatis
mybatis-spring
2.0.2




org.springframework
spring-webmvc
5.1.9.RELEASE


org.springframework
spring-jdbc
5.1.9.RELEASE



3.Maven资源过滤设置




src/main/resources

**/*.properties
**/*.xml

true


src/main/java

**/*.properties
**/*.xml

false


 4.建立基本结构和配置框架

com.luo.pojo

com.luo.controller

com.luo.service

 BookService

 BookServiceImpl

com.luo.dao

 BookMapper

 BookMapper.xml

mybatis-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">



 

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">

Mybatis层编写

1.数据库配置文件:database.properties

mysql8.0+加一个时区的配置&serverTimezone=Asia/Shanghai

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username=root
password=123456

2.IDEA关联数据库

3.编写Mybatis核心配置文件

4.编写数据库对应的实体类:com.luo.pojo.Books(使用了lombok插件)

 5.编写Dao层的Mapper接口

6.编写接口对应的Mapper.xml文件(需要导入Mybatis包)

7.编写service层的接口和实现类:

接口:

实现类:

此时项目结构

 此时底层需求操作编写完毕!