SSM框架整合
1、建立简单的maven项目
2、导入依赖
<?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>
<groupId>org.example</groupId>
<artifactId>SSM</artifactId>
<version>1.0-SNAPSHOT</version>
<!--导入依赖 Junit 数据库驱动 连接池 servlet jsp mybatis mybatis-spring spring -->
<dependencies>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!---->
</dependencies>
<!--静态资源导出问题-->
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</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>
</project>
3、建立数据库连接(在schemas中选择连接的数据库)
4、建立相应的包
5、编写配置文件
- mybatis核心配置文件框架
<?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>
- spring核心配置文件框架
<?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>
将连接数据库的一些操作封装起来
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&serverTimezone=Asia/Shanghai
username=root
password=root
在mybatis核心配置文件中引入
<?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>
<!--配置数据源 交给spring去做-->
<!--起别名-->
<typeAliases>
<typeAlias type="com.zheng.pojo.Books" alias="Books"/>
</typeAliases>
<!--在mybatis核心配置文件中注册mapper文件-->
<mappers>
<mapper class="com.zheng.dao.BookMapper"/>
</mappers>
</configuration>
编写实体类
package com.zheng.pojo;
public class Books {
private int bookId;
private String bookName;
private int bookCounts;
private String detail;
public Books( ) {
}
public Books(int bookId, String bookName, int bookCounts, String detail) {
this.bookId = bookId;
this.bookName = bookName;
this.bookCounts = bookCounts;
this.detail = detail;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookCounts() {
return bookCounts;
}
public void setBookCounts(int bookCounts) {
this.bookCounts = bookCounts;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
@Override
public String toString() {
return "Books{" +
"bookId=" + bookId +
", bookName='" + bookName + '\'' +
", bookCounts=" + bookCounts +
", detail='" + detail + '\'' +
'}';
}
}
编写dao层
package com.zheng.dao;
import com.zheng.pojo.Books;
import java.util.List;
public interface BookMapper {
/**
* 增加一本书
*/
int addBook(Books books);
/**
* 删除一本书
*/
int deleteBookById(int id);
/**
* 更新一本书
*/
int updateBook(Books books);
/**
* 查询所有的书籍
*/
List<Books> queryAllBook();
/**
* 查询一本书籍通过id
*/
Books queryBookById(int id);
}
编写对应的mapper文件
<?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.zheng.dao.BookMapper">
<!--* 增加一本书-->
<insert id="addBook" parameterType="Books">
insert into ssmbuild.books (bookId,bookName,bookCounts,detail)
values(#{bookId},#{bookName},#{bookCounts},#{detail})
</insert>
<!--* 删除一本书-->
<delete id="deleteBookById" parameterType="int">
delete from ssmbuild.books where bookId=#{bookId}
</delete>
<!-- * 更新一本书-->
<update id="updateBook" parameterType="Books">
update ssmbuild.books set bookId=#{bookId},bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookId=#{bookId}
</update>
<!--* 查询所有的书籍-->
<select id="queryAllBook" parameterType="Books">
select * from ssmbuild.books
</select>
<!--* 查询一本书籍通过id-->
<select id="queryBookById" resultType="Books">
select * from ssmbuild.books where bookId=#{bookId}
</select>
</mapper>
编写service层(service调用dao层)
接口
package com.zheng.service;
import com.zheng.pojo.Books;
import java.util.List;
public interface BookService {
/**
* 增加一本书
*/
int addBook(Books books);
/**
* 删除一本书
*/
int deleteBookById(int id);
/**
* 更新一本书
*/
int updateBook(Books books);
/**
* 查询所有的书籍
*/
List<Books> queryAllBook();
/**
* 查询一本书籍通过id
*/
Books queryBookById(int id);
}
编写接口实现子类
package com.zheng.service;
import com.zheng.dao.BookMapper;
import com.zheng.pojo.Books;
import java.util.List;
public class BookServiceImpl implements BookService{
//业务层调用dao层,组合dao
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
public int addBook(Books books) {
return bookMapper.addBook(books);
}
public int deleteBookById(int id) {
return bookMapper.deleteBookById(id);
}
public int updateBook(Books books) {
return bookMapper.updateBook(books);
}
public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
}
整合mybatis
核心配置文件
mybatis-config.xml
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置数据源 交给spring去做-->
<!--起别名-->
<typeAliases>
<typeAlias type="com.zheng.pojo.Books" alias="Books"/>
</typeAliases>
<!--在mybatis核心配置文件中注册mapper文件-->
<mappers>
<mapper class="com.zheng.dao.BookMapper"/>
</mappers>
</configuration>
database.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root
整合spring
- 整合dao接口
spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--1、关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<!--2、连接池
dbcp:半自动连接,不能自动连接
c3p0:自动化操作,自动化的加载配置文件,并且可以自动设置到对象中
druid:hikari
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--c3p0连接池的私有属性-->
<property name="maxPoolSize" value="30"></property>
<property name="minPoolSize" value="10"/>
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="10000"/>
<!--当获取连接失败重试次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--3、sqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定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 name="basePackage" value="com.zheng.dao"/>
</bean>
</beans>
- 整合service接口
spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描service下的包-->
<context:component-scan base-package="com.zheng.service"/>
<!--2、将我们的所有业务类,注入到spring,可以通过配置,或者注解实现-->
<bean id="BookServiceImpl" class="com.zheng.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!--3、声明事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
整合spingmvc
1、给maven项目加入框架
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">
<!--配置DispatchServlet;这个是springMVC的核心,请求分发器,前端控制器-->
<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:applicationContext.xml</param-value>
</init-param>
<!--启动级别-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--在springMVC中,/:代表匹配所有的请求,不包括jsp页面。/*:匹配所有的请求,包括jsp-->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--乱码过滤-->
<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>
</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
http://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、静态资源过滤-->
<mvc:default-servlet-handler/>
<!--3、自动扫描包(controller)-->
<context:component-scan base-package="com.zheng.controller"/>
<!--4、视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
测试
package com.zheng.controller;
import com.zheng.pojo.Books;
import com.zheng.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
//查询所有的书籍,并且返回一个书籍展示页面
@RequestMapping("/queryAll")
public String list(Model model){
List<Books> books = bookService.queryAllBook();
model.addAttribute("list:",books);
return "allBook";
}
}
遇到的问题:
1、lib文件的位置和class平级
applicationContext
<?xml version="1.0" encoding="UTF8"?>
<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">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>
2、编码字节无效
。将resources配置文件的utf-8写成utf8
mapper文件中的也要修改
3、在web.xml文件中地址位置改掉
<!--里边的路径是编写的配置文件-->
<param-value>classpath:applicationContext.xml</param-value>