SSM框架整合图书管理项目
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>
SSM框架整合图书管理项目的更多相关文章
- ssm框架整合+maven项目创建
在引入外部maven插件后就可以创建一个maven项目了,这篇文章主要介绍ssm框架的整合和如何创建一个maven项目 1.在开发工具的项目空白区单击右键,依次选择New.Other,会出现如下界面, ...
- SSM框架整合项目 :租房管理系统
使用ssm框架整合,oracle数据库 框架: Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastj ...
- 使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化
一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...
- 【转载】使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化
一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...
- SpringMVC详解及SSM框架整合项目
SpringMVC ssm : mybatis + Spring + SpringMVC MVC三层架构 JavaSE:认真学习,老师带,入门快 JavaWeb:认真学习,老师带,入门快 SSM框架: ...
- JavaWeb之ssm框架整合,用户角色权限管理
SSM框架整合 Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastjson 5, aspectwea ...
- 基于maven的ssm框架整合
基于maven的ssm框架整合 第一步:通过maven建立一个web项目. 第二步:pom文件导入jar包 (1 ...
- SSM框架整合搭建教程
自己配置了一个SSM框架,打算做个小网站,这里把SSM的配置流程详细的写了出来,方便很少接触这个框架的朋友使用,文中各个资源均免费提供! 一. 创建web项目(eclipse) File-->n ...
- SSM框架整合(实现从数据库到页面展示)
SSM框架整合(实现从数据库到页面展示) 首先创建一个spring-web项目,然后需要配置环境dtd文件的引入,环境配置,jar包引入. 首先让我来看一下ssm的基本项目配件.(代码实现) 1.首先 ...
随机推荐
- 一文带你了解webrtc基本原理(动手实现1v1视频通话)
webrtc (Web Real-Time Communications) 是一个实时通讯技术,也是实时音视频技术的标准和框架. 大白话讲,webrtc是一个集大成的实时音视频技术集,包含了各种客户端 ...
- MultiJittered采样类定义和测试
多重抖动在书上说的是水平和竖直方面随机交换. 类声明: #pragma once #ifndef __MULTIJITTERED_HEADER__ #define __MULTIJITTERED_HE ...
- 关于KeyFile的破解,含注册机源代码
程序来自于<加密与解密3>的第五章的PacMe.exe.书中并没有给出C语言实现的加密与解密代码,自己花了一些时间,把代码还原了,并且写了一个C语言的注册机. 加密原理:正如书中所说,此程 ...
- 点击>>>解锁Apache Hadoop Meetup 2021!
" 10月16日,属于开源发烧友的狂欢日来啦! Apache Hadoop Meetup 2021 开源大数据行业交流盛会盛大开启!让我们相约北京,一起嗨翻初秋~ 在当今信息化时代,逐渐成熟 ...
- LuoguU72177 火星人plus (逆康拓展开)
没开long long见祖宗... BIT先求逆序对来造表存展开关系,线段树维护01进制 #include <iostream> #include <cstdio> #incl ...
- feign远程调用出错
如果你传递的参数,比较复杂时,默认会采用POST的请求方式. 传递单个参数时,推荐使用@PathVariable,如果传递的单个参数比较多,这里也可以采用@RequestParam,Feign接口中不 ...
- Hnoi2014世界树
题面 说明/提示 N<=300000, q<=300000,m[1]+m[2]+...+m[q]<=300000 题解 这道题一看 "m[1]+m[2]+...+m[q]& ...
- c# 异步进阶————channel [一]
前言 该系列为异步编程的进阶篇,其实也不能这么讲.世界上本没有进阶篇,只能说是高级篇(高级篇不能说多高级,是对底层的封装的意思),只要是加深理解都是进阶. 本章先介绍一下channel. 正文 下面没 ...
- helm安装metrics-server-3.8.2
Application version 0.6.1 Chart version 3.8.2 获取chart包 helm repo add metrics-server https://kubernet ...
- KingbaseES的SQL语句-CTE递归
背景 从上下级关系表中,任意一个节点数据出发,可以获得该节点的上级或下级.CTE的递归语法,或者 connect by 与 start with的 查询语法,能够实现这个需求. 当我们需要制作上下级关 ...