SSM框架整合一:MyBatis
基本环境搭建
1.新建一个Maven项目,添加web模块的支持
2,导入相关的pom依赖
junit
junit
4.13
test
mysql
mysql-connector-java
5.1.47
com.mchange
c3p0
0.9.5.5
javax.servlet
servlet-api
2.5
javax.servlet.jsp
jsp-api
2.2
javax.servlet
jstl
1.2
org.mybatis
mybatis
3.5.2
org.mybatis
mybatis-spring
2.0.2
org.springframework
spring-webmvc
5.2.7.RELEASE
org.springframework
spring-jdbc
5.2.7.RELEASE
org.projectlombok
lombok
1.18.12
3.Maven资源过滤设置
src/main/java
**/*.properties
**/*.xml
false
src/main/resourses
**/*.properties
**/*.xml
false
3.项目基本框架
配置MyBatis
1.创建配置文件database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
2.配置mybatis
/p>
"http://mybatis.org/dtd/mybatis-3-config.dtd">
3.实体类,此处使用了lombok
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books
{
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
4.接口类
package com.study.dao;
import com.study.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBookById(@Param("bookID") int id);
//更新一本书
int updateBook(Books books);
//查询一本书
Books queryBookById(@Param("bookID") int id);
//查询全部的书
List queryAllBook();
}
5.Mapper文件
/p>
PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into ssm.books (bookName, bookCounts, detail )
values (#{bookName},#{bookCounts},#{detail})
delete from ssm.books where bookID = #{bookId}
update ssm.books
set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookID=#{bookID}
select * from ssm.books
where bookID = #{bookId}
select * from ssm.books
SSM框架整合二:Spring
Spring配置文件
Spring配置文件,此处使用c3p0数据源
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
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
Spring整合Service层
1.Service层接口类
public interface BookService {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBookById(int id);
//更新一本书
int updateBook(Books books);
//查询一本书
Books queryBookById(int id);
//查询全部的书
List queryAllBook();
}
2.Service层接口实现类
@Service
public class BookServiceImpl implements BookService {
//Service 调用Dao层,组合Dao
@Autowired
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 Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
public List queryAllBook() {
return bookMapper.queryAllBook();
}
}
3.Spring Service配置xml
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
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
SSM框架整合三:SpringMVC
1.框架web.xml配置文件设置
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">
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
springmvc
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encodingFilter
/*
15
2.SpringMVC配置文件
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
https://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">
3.Spring配置文件applicationContext.xml导入
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
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
至此SSM框架整合完成。