ssm整合
- 镇楼
- 一、基本流程
- 二、ssm整合
镇楼
本次整合做的是图书的增删改查
一、基本流程
二、ssm整合
1.数据库
1.1创建数据库
1创建数据库ssmbuild
//创建数据库
CREATE DATABASE ssmbuild
//切换数据库
USE ssmbuild;
1.2建表
//如果数据库中存在books表,就把它从数据库中drop掉。
DROP TABLE IF EXISTS books;
//CREATE TABLE 建表 not null不能为空 COMMENT 是备注、注释的意思
//auto_increment 自增 Engine =innodb default charset=utf8 默认编码字符集
CREATE TABLE books(
-> bookId int(10) not null auto_increment comment '书id',
-> bookName varchar(100) not null comment '书名',
-> bookCounts int(11) not null comment '数量',
-> detail varchar(200) not null comment '描述',
-> PRIMARY KEY (bookId))
-> Engine =innodb default charset=utf8;
//插入信息
insert into books (bookId,bookName,bookCounts,detail) values
(1,'java',1,'重入门到放弃'),
(2,'mysql',10,'重删库到跑路'),
(3,'javaScript',20,'从进门就出逃');
books表:
2.新建一Maven项目!
2.1、导入相关的pom依赖!
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!--数据库驱动-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- 数据库连接池 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--Spring-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.10</version>
</dependency>
</dependencies>
2.2、查看链接数据库
根据字节的数据库来
测试成功:
选中创建的数据库,楼主已经建好就不建了。
查看我们建立的数据库表:记得刷新
2.3、Maven资源过滤设置
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
2.4、搭建基本的框架
基本架构
2.4.1、资源文件
2.4.2、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>
<!--settings:控制mybatis全局行为,还有其他设置-->
<settings>
<!--设置mybatis输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--设置别名-->
<typeAliases>
<!--name:实体类所在的包名-->
<package name="com.opnelab.pojo"/>
</typeAliases>
</configuration>
2.4.3、applicationContext.xml(楼主自用模板)
本模板包含spring-dao和spring-service。可以直接用这个,或分开,然后导入过来。
楼主自用的模板:东西都是配好的改改就行 数据库参数名字相同的话能够直接用
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 1.配置数据源 -->
<!-- 关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<!-- 1.配置数据源 -->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<!--连接数据库的url -->
<property name="url" value="${jdbc.url}"></property>
<!--连接数据库的用户名 -->
<property name="username" value="${jdbc.username}"></property>
<!--连接数据库的密码 -->
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 声明的是mybatis中提供的SqlSessionFactoryBean类,这个给类内部创建SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- mybatis主配置文件的位置
configLocation是Resource类型,读取配置文件
它的复制,使用value,指定文件路径,使用的是classpath:文件路径-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath*:com/openlab/dao/BookMapper.xml"></property>
</bean>
<!-- 创建dao对象,使用sqlSession的getMapper(UserDao.class)
MapperScannerConfigurer:内部调用getMapper()生成每个Dao的代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定qlSessionFactory对象的name-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指定包名,名字是dao接口所在的的包名
MapperScannerConfigurer会扫描这个包所有的接口,把每个接口执行一次getMapper()方法得到每个dao对象.
创建好的对象放入到Spring容器中-->
<property name="basePackage" value="com.openlab.dao"/>
</bean>
<!-- 扫描器:扫描指定的包-->
<context:component-scan base-package="com.openlab"/>
<!-- Spring事物 注解实现-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
applicationContext.xml:这里没截图到就在网上找的:
2.4.4、database.properties(楼主自用模板)
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
#//mysql8版本: leopard?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai
#//useSSL=true 安全连接 useUnicode=true&characterEncoding=utf8编码字符集
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
2.5、创pojo
2.5.1、package com.openlab.pojo;
package com.openlab.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookId;
private String bookName;
private String bookCount;
private int detail;
}
在pom文件中添加了注解包:
<!-- 注解-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
2.5.2、创建BookMapper
创建接口BookMapper
package com.openlab.dao;
import com.openlab.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
//增加一本书
int addBook(Books book);
//删除一本书
int deleteBookById(@Param("bookId") int id);
//更新一本书
int updateBooks(Books books);
//查询一本书
Books queryBookById(int id);
//查询全部的书
List<Books> queryAllBook();
}
2.5.3、创建对应的Mapper.xml
一个Mapper对应一个接口
楼主的Mapper.xml模板
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="">
</mapper>
楼主写的对应的BookMapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.openlab.dao.BookMapper">
<!--添加一本书 parameterType参数类型-->
<insert id="addBook" parameterType="com.openlab.pojo.Books">
insert into ssmbuild.books (bookName, bookCounts, detail)
values (#{bookName},#{bookCounts},#{detail};
</insert>
<!-- 删除一本书-->
<delete id="deleteBookById" parameterType="java.lang.Integer">
delete from ssmbuild.books where bookId=#{bookId};
</delete>
<!-- //更新一本书-->
<update id="updateBooks" parameterType="com.openlab.pojo.Books" >
update ssmbuild.books
set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookId=#{bookId} ;
</update>
<!-- //查询一本书 resultType="com.openlab.pojo.Books" 返回类型参数-->
<select id="queryBookById" resultType="com.openlab.pojo.Books" parameterType="java.lang.Integer">
select * from ssmbuild.books where bookId=#{bookId};
</select>
<!-- //查询全部的书-->
<select id="queryAllBook" resultType="com.openlab.pojo.Books" >
select * from ssmbuild.books ;
</select>
</mapper>
2.6、业务层Service
2.6.1、业务也是dao里面操作
package com.openlab.service;
import com.openlab.pojo.Books;
import java.util.List;
public interface BookServie {
//增加一本书
int addBook(Books book);
//删除一本书
int deleteBookById(int id);
//更新一本书
int updateBooks(Books book);
//查询一本书
Books queryBookById(int id);
//查询全部的书
List<Books> queryAllBook();
}
2.6.2、业务层的实现类
创建实现类的包
BookServiceImpl 类
package com.openlab.service.Impl;
import com.openlab.dao.BookMapper;
import com.openlab.pojo.Books;
import com.openlab.service.BookServie;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Setter
@Service
public class BookServiceImpl implements BookServie {
// 用业务层掉dao层。
@Autowired
private BookMapper bookMapper;
@Override
public int addBook(Books book) {
return bookMapper.addBook(book);
}
@Override
public int deleteBookById(int id) {
return bookMapper.deleteBookById(id);
}
@Override
public int updateBooks(Books book) {
return bookMapper.updateBooks(book);
}
@Override
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
@Override
public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}
}
2.7、Spring层
2.7.1、配置Spring整合MyBatis,我们这里数据源使用druid连接池;
2.7.2、我们去编写Spring整合Mybatis的相关的配置文件;spring-dao.xml(楼主自用模板)
楼主给的模板都给你们配好了 改改参数就ok 楼主用的druid连接池。
也就是楼主给的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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<!-- 1.配置数据源 -->
<!-- 关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<!-- 1.配置数据源 -->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<!--连接数据库的url -->
<property name="url" value="${jdbc.url}"></property>
<!--连接数据库的用户名 -->
<property name="username" value="${jdbc.name}"></property>
<!--连接数据库的密码 -->
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 声明的是mybatis中提供的SqlSessionFactoryBean类,这个给类内部创建SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- mybatis主配置文件的位置
configLocation是Resource类型,读取配置文件
它的复制,使用value,指定文件路径,使用的是classpath:文件路径-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 创建dao对象,使用sqlSession的getMapper(UserDao.class)
MapperScannerConfigurer:内部调用getMapper()生成每个Dao的代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定qlSessionFactory对象的name-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指定包名,名字是dao接口所在的的包名
MapperScannerConfigurer会扫描这个包所有的接口,把每个接口执行一次getMapper()方法得到每个dao对象.
创建好的对象放入到Spring容器中-->
<property name="basePackage" value="com.openlab.dao"/>
</bean>
<!-- 扫描器:扫描指定的包-->
<context:component-scan base-package="com.openlab"/>
<!-- Spring事物 注解实现-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
2.7.3、applicationContext.xml分离数据库链接spring-dao.xml以及Spring-servoice.xml
为了代码的可读性讲数据库链接分离出来 applicationContext.xml就为空
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 1.配置数据源 -->
<!-- 关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<!-- 1.配置数据源 -->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<!--连接数据库的url -->
<property name="url" value="${jdbc.url}"></property>
<!--连接数据库的用户名 -->
<property name="username" value="${jdbc.username}"></property>
<!--连接数据库的密码 -->
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 声明的是mybatis中提供的SqlSessionFactoryBean类,这个给类内部创建SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- mybatis主配置文件的位置
configLocation是Resource类型,读取配置文件
它的复制,使用value,指定文件路径,使用的是classpath:文件路径-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath*:com/openlab/dao/BookMapper.xml"></property>
</bean>
<!-- 创建dao对象,使用sqlSession的getMapper(UserDao.class)
MapperScannerConfigurer:内部调用getMapper()生成每个Dao的代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定qlSessionFactory对象的name-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指定包名,名字是dao接口所在的的包名
MapperScannerConfigurer会扫描这个包所有的接口,把每个接口执行一次getMapper()方法得到每个dao对象.
创建好的对象放入到Spring容器中-->
<property name="basePackage" value="com.openlab.dao"/>
</bean>
</beans>
Spring-servoice.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 扫描器:扫描指定的包-->
<context:component-scan base-package="com.openlab.service"/>
<!-- Spring事物 注解实现-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
Spring-servoice.xml关联spring-dao.xml
爆红的话关联一下:
或者以下 能达到同样的效果
<import resource="Spring-dao.xml" />
BookServiceImpl中之前写的时候就可以直接注释,就不用在Spring-servoice.xml中导入
2.8、SpringMVC层
2.8.1、增加web支持
2.8.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">
<!-- 注册DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 关联一个Springmvc的配置文件:-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 启动级别 立即加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 匹配所有请求: /(不包括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>
<!-- session-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
2.8.2、配置spring-mvc.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">
<!--DispatcherServlet-->
<servlet>
<servlet-name>DispatcherServlet</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>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--encodingFilter-->
<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>
<!--Session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
2.8.3、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">
<import resource="classpath:spring/spring-dao.xml"/>
<import resource="classpath:spring/spring-service.xml"/>
<import resource="classpath:spring/springmvc-servlet.xml"/>
</beans>
2.8.4、创建jsp文件夹
2.8.5、Controller
Controller
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
@RequestMapping("/allBook")
public String list(Model model) {
List<Books> list = bookService.queryAllBook();
model.addAttribute("list", list);
return "allBook";
}
}
2.8.5、index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE HTML>
<html>
<head>
<title>首页</title>
<style type="text/css">
a {
text-decoration: none;
color: black;
font-size: 18px;
}
h3 {
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: deepskyblue;
border-radius: 4px;
}
</style>
</head>
<body>
<h3>
<a href="${pageContext.request.contextPath}/book/allBook">点击进入列表页</a>
</h3>
</body>
</html>
2.8.6、allBook.jsp
<%@ page import="org.springframework.ui.Model" %><%--
Created by IntelliJ IDEA.
User: xiaoyaoge
Date: 2021/9/21
Time: 11:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${list}
</body>
</html>
运行结果
3、楼主模板:保存下 下次直接拿来改
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 1.配置数据源 -->
<!-- 关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<!-- 1.配置数据源 -->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<!--连接数据库的url -->
<property name="url" value="${jdbc.url}"></property>
<!--连接数据库的用户名 -->
<property name="username" value="${jdbc.username}"></property>
<!--连接数据库的密码 -->
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 声明的是mybatis中提供的SqlSessionFactoryBean类,这个给类内部创建SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- mybatis主配置文件的位置
configLocation是Resource类型,读取配置文件
它的复制,使用value,指定文件路径,使用的是classpath:文件路径-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath*:com/openlab/dao/BookMapper.xml"></property>
</bean>
<!-- 创建dao对象,使用sqlSession的getMapper(UserDao.class)
MapperScannerConfigurer:内部调用getMapper()生成每个Dao的代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定qlSessionFactory对象的name-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指定包名,名字是dao接口所在的的包名
MapperScannerConfigurer会扫描这个包所有的接口,把每个接口执行一次getMapper()方法得到每个dao对象.
创建好的对象放入到Spring容器中-->
<property name="basePackage" value="com.openlab.dao"/>
</bean>
</beans>
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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 扫描器:扫描指定的包-->
<context:component-scan base-package="com.openlab.service"/>
<!-- Spring事物 注解实现-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<bean id="transactionManager" class="org.springframework.jdbc.support.JdbcTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
springmvc-servlet.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 https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--不用注解导包-->
<!-- <bean id="/hello" class="com.openlab.controller.HelloController" />-->
<!-- 处理器映射器 -->
<!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />-->
<!-- Adapter适配 HandlerAdapter处理器适配器:适配相应的处理器-->
<!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />-->
<!-- 注解 自动扫描包,让包下的注解生效,由IOC容器统一管理-->
<context:component-scan base-package="com.openlab.controller"/>
<!-- 让SpringMVC不处理静态资源 .css .js .html .mp4-->
<mvc:default-servlet-handler/>
<!-- 支持MVC注解驱动-->
<!-- 在Spring中一般采用@RequestMapping注解完成映射关系-->
<!-- 想要使@RequestMapping生效-->
<!-- 必须上下文中注册DefaultAnnotationHandlerMapping-->
<!-- 和一个AnnotationMethodHandlerAdaapter实例-->
<!-- 这两个是分别在类级别和方法级别处理-->
<!-- 而Annotation-driver配置帮助我们自动完成上述两个实例的注入-->
<mvc:annotation-driven/>
<!-- 视图解析器DisplatcherServlet 给他的ModelAbdView-->
<!-- 获取了ModeAndeView是数据-->
<!-- 解析ModeAndView视图的名字-->
<!-- 拼接视图的名字找到相应的视图 hello-->
<!-- 将数据渲染到视图上-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
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>
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>
<!--settings:控制mybatis全局行为,还有其他设置-->
<settings>
<!--设置mybatis输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--设置别名-->
<typeAliases>
<!--name:实体类所在的包名-->
<package name="com.opnelab.pojo"/>
</typeAliases>
</configuration>
database.properties
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
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">
<!--DispatcherServlet-->
<servlet>
<servlet-name>DispatcherServlet</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>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--encodingFilter-->
<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>
<!--Session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
!!!!看过来看过来!!!
看完觉得有用的大大们!!!!!