SSM 框架整合过程总结,书籍 + 视频 + 学习笔记 + 技能提升资源库
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
database.properties(数据库连接资源文件)
jdbc.driver=com.mysql.jdbc.Driver
mysql 8.0 及以上驱动包位置发生了改变要这样写:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名?useSSL=true&useUnicode=true&characterEncoding=utf8
mysql8.0 及以上 url 也发生了一点点改变,应该这样写:
jdbc.url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
jdbc.username=用户名
jdbc.password=用户密码
[](
)步骤五:编写 Mybatis 层代码
[](
)1.编写实体类
实体类根据 ORM 原则进行编写,这里我们用 lombok 节约时间
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
[](
)2.编写表对应的 Mapper 接口
如上述的 BooksMapper,并且这里我们采用注解的方式来快速开发
public interface BooksMapper {
//增加一本书
@Insert("insert into books(bookName, bookCounts, detail) VALUES (#{bookName},#{bookCounts},#{details})")
int addBooks(Books books);
//删除一本书
@Delete("delete from books where bookID=#{id}")
int deleteBookById(@Param("id") int id);
//更新一本书
@Update("update books set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}")
int updateBooks(Books books);
//查询一本书
@Select("select * from books where bookID=#{id}")
Books queryBooksById(@Param("id") int id);
//查询全部书
@Select("select * from books")
List<Books> queryAllBooks();
}
[](
)3.在 resource 下编写对应的 Mapper.xml 文件
这里我们创建 BooksMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zyx.dao.BookMapper">
</mapper>
[](
)4.将 Mapper 绑定到 Mybatis 配置文件中
也就是我们这里的 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.zyx.pojo"/>
</typeAliases>
<mappers>
<mapper resource="BooksMapper.xml"/>
</mappers>
</configuration>
就此 Mybatis 层代码全部搞定!
[](
)5.编写 service 层代码
(1)编写 service 接口
这里是 BooksService
public interface BooksService {
//增加一本书
int addBooks(Books books);
//删除一本书
int deleteBookById(int id);
//更新一本书
int updateBooks(Books books);
//查询一本书
Books queryBooksById(int id);
//查询全部书
List<Books> queryAllBooks();
}
(2)编写接口实现类
这里是 BooksServiceImpl
public class BooksServiceImpl implements BooksService{
//service 层调 dao,所以需要有一个 dao 层的 mapper 对象
private BooksMapper booksMapper;
//为了方便 Spring 对它进行注入,写上 set 方法
public void setBookMapper(BooksMapper booksMapper) {
this.booksMapper = booksMapper;
}
public int addBooks(Books books) {
return booksMapper.addBooks(books);
}
public int deleteBookById(int id) {
return booksMapper.deleteBookById(id);
}
public int updateBooks(Books books) {
return booksMapper.updateBooks(books);
}
public Books queryBooksById(int id) {
return booksMapper.queryBooksById(id);
}
public List<Books> queryAllBooks() {
return booksMapper.queryAllBooks();
}
}
就此 Mybatis 层代码全部完成
[](
)步骤六:编写 Spring 层代码
[](
)1.编写 Spring 整合 DAO 层的资源文件
就是这里的 Spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:property-placeholder location="classpath:database.properties"/>
<!--2.数据源(种类有)
dbcp:半自动化操作,不能自动连接,需要手动连接
c3p0:自动化操作,自动化加载配置文件,并且可以自动设置到对象中
druid
hikari
DriverManngerDateSource Spring 默认数据源
这里我们使用 C3P0
-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"
/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<property name="autoCommitOnClose" value="false"/>
<property name="checkoutTimeout" value="10000"/>
<property name="acquireRetryAttempts" value="2"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.zyx.dao"/>
</bean>
</beans>
[](
)2.编写 Spring 整合 service 层资源文件
这里的文件为 Spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.zyx.service"/>
<!--2.将业务类注入到 Spring 容器中,可以使用配置,也可以使用注解
注解则不需要以下代码
-->
<bean class="com.zyx.service.BookServiceImpl" id="bookService">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
注:使用注解要在接口实现类上方加上 @Service
到此 Spring 层代码全部完成!
[](
)步骤七:编写 SpringMVC 层代码
[](
)1.增加 web 支持,并创建所需要的文件夹
这里比较简单不过多解释
2.编写 SpringMVC 配置资源文件
我们这里为 Spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.zyx.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
[](
)3.编写 web.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
评论