SSM整合
1、MyBatis层
1、创建一个存放书籍数据的数据库
use smbms
CREATE TABLE books(
bookID int(10) PRIMARY key AUTO_INCREMENT not null COMMENT '书id',
bookName VARCHAR(100) not null comment '书名',
bookCounts INT(11) not null comment '数量',
detail VARCHAR(200) not null comment '描述'
)
INSERT into books(bookID,bookName,bookCounts,detail) VALUES
(1,'Java',1,'从入门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到经牢');
2、Maven项目依赖
<dependencies>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--===================================================-->
<!--MySQL层-->
<!--MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!--数据库连接池:c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--===================================================-->
<!--MVC层-->
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--JSP-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!--===================================================-->
<!--EL表达式-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--注解-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--==============================================-->
<!--mybatis层-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--==============================================-->
<!--spring层-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.5</version>
</dependency>
<!--spring-webmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.19.RELEASE</version>
</dependency>
</dependencies>
<!--===================================================-->
<!--静态资源导出问题-->
<build>
<resources>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
3、连接数据库
4、创建Spring核心配置文件applicationContext.xml,数据库连接配置文件database.properties,mybatis核心配置文件mybatis-config.xml
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
database.properties
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/smbms?useSSL=true&useUnicode=true&characterEncoding=utf-8
prop.username=root
prop.password=tanning000
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>
</configuration>
5、创建实体类
注意:在创建实体类的时候,必须与数据库中的名字保持一致,可更改大小写,且数据库中的属性名也保持一致
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
**注意:
- 实体类中的属性名称一定要与数据库中的名称保持一致
- 如果数据库中使用到的名称中含有下划线,在配置中添加驼峰命名法即可
<settings> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings>
**
2、Spring层
6、创建Dao层接口以及接口的xml
1、创建接口BookMapper
public interface BookMapper {
//增加一本书
int addBookById(Books books);
//删除一本书
int deleteBookById(@Param("bookId") int id);
//更新一本书
int updateBookById(Books books);
//查询一本书
Books queryBookById(@Param("bookId")int id);
//查询全部书籍
List<Books> queryAllBook();
}
2、BookMapper.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="Parent.dao.BookMapper">
<insert id="addBookById" parameterType="Entity.Books">
insert into Books(bookName,bookCounts,detail)
values (#{bookName},#{bookCounts},#{details});
</insert>
<delete id="deleteBookById" parameterType="int">
delete from Books where bookID = #{bookId}
</delete>
<update id="updateBookById" parameterType="Entity.Books">
update Books
set bookName=#{bookName},bookCounts=# {bookCounts},detail=#{detail}
where bookID=#{bookId};
</update>
<select id="queryBookById" resultType="Entity.Books">
select * from Books where bookID=#{bookId};
</select>
<select id="queryAllBook" resultType="Entity.Books">
select * from Books;
</select>
</mapper>
7、创建Service层
1、创建接口BookService
public interface BookService {
//增加一本书
int addBookById(Books books);
//删除一本书
int deleteBookById(int id);
//更新一本书
int updateBookById(Books books);
//查询一本书
Books queryBookById(int id);
//查询全部书籍
List<Books> queryAllBook();
}
2、创建接口的实现类
public class BookServiceImpl implements BookService{
//service层调用dao层:组合Dao
//注:将接口注入到业务层,是通过MapperScannerConfigurer扫描器扫描mapper所在的包之后,在通过@autowried注入到IOC容器中
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
public int addBookById(Books books) {
return bookMapper.addBookById(books);
}
public int deleteBookById(int id) {
return bookMapper.deleteBookById(id);
}
public int updateBookById(Books books) {
return bookMapper.updateBookById(books);
}
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}
}
8、编写MyBais和Spring的整合配置文件
1、Spring-dao.xml
注:spring跟mybatis整合时,mybatis底层会装配SQLsessionFactoryBean、dataSource
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--1、关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<!--2、配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${prop.driverClass}"></property>
<property name="jdbcUrl" value="${prop.url}"></property>
<property name="user" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
<!--3、sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--绑定MyBatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!--4、配置dao接口扫描包,动态实现Dao接口可以注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!--注入要扫描的包-->
<property name="basePackage" value="Parent.dao"></property>
</bean>
</beans>
注:c3p0连接池的私有属性
9、编写Service和Spring的整合配置文件
1、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"
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">
<!--1、添加自动扫描器-->
<context:component-scan base-package="Parent.Service"/>
<!--2、将所有的业务类,注入到spring中,可以通过配置,或者通过注解实现-->
<bean id="BookServiceImpl" class="Parent.Service.BookServiceImpl">
<property name="bookMapper" value="Parent.Service.BookService"/>
</bean>
<!--3、声明式事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
注意:在写三层架构整合文件的时候,如果文件之间没有关联,会出现错误
解决方法为:
1、在Project Structure中找到model,查看是否有三个整合的配置文件
点击即可加入
2、可以在applicationContext.xml配置文件中注入其他配置文件
3、SpringMVC层
1、添加web框架
2、编写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">
<!--1、配置DispatcherServlet-->
<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>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--2、设置乱码过滤-->
<filter>
<filter-name>encodingFilter</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>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--3、设置Session的过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
3、编写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
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1、注解驱动-->
<mvc:annotation-driven/>
<!--2、静态资源过滤(html\js)-->
<mvc:default-servlet-handler/>
<!--3、扫描包-->
<context:component-scan base-package="Parent.Controller"/>
<!--4、视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!--后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
**
注意:
- 此页面只展示了三层框架之间的整合,并未展现出框架整合之后的业务代码
- 相关Controller层的业务代码,等后续时间充裕在进行补上
**