1.创建数据库
CREATE DATABASE `ssmbuild`;
USE `ssmbuild`;
DROP TABLE IF EXISTS `books`;
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 '描述',
KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'Java基础巩固'),
(2,'MySQL',10,'mysql必知必会'),
(3,'Linux',5,'常用命令');
2.新建Maven项目,整合pom依赖
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</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-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<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>
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>
3.建立基本结构和配置框架
1.编写常用package
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>
</configuration>
3.application-context.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>
4.mybatis层编写
1.编写com.ssm.pojo,使用lombok插件
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Books {
private int bookId;
private String bookName;
private int bookCounts;
private String detail;
}
2.编写dao层的Mapper接口
public interface BookMapper {
/**
* 添加图书信息
* @param book
* @return
*/
public int addBook(Books book);
/**
* 更新图书信息
* @param book
* @return
*/
public int updateBook(Book book);
/**
* 根据图书编号删除图书信息
* @param bookId
* @return
*/
public int deleteBookByBookId(int bookId);
/**
* 根据图书编号查询图书信息
* @param bookId
* @return
*/
public Books queryBookByBookId(int bookId);
/**
* 查询所有图书信息
* @return
*/
public List<Books> queryBooks();
}
3.编写接口对应的 Mapper.xml 文件。需要导入MyBatis的包
<?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.ssm.dao.BooksMapper">
<insert id="addBook" parameterType="books">
insert into books(bookName,bookCounts,detail)
values(#{bookName},#{bookCounts},#{detail})
</insert>
<delete id="deleteBookByBookId" parameterType="int">
delete from books where bookId = #{bookId}
</delete>
<update id="updateBook" parameterType="books">
update books set bookName = #{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookId = #{bookId}
</update>
<select id="queryBookByBookId" resultType="books" parameterType="int">
select * from books where bookId = #{bookId}
</select>
<select id="queryBooks" resultType="books">
select * from books
</select>
</mapper>
4.在mybatis-config.xml中声明mapper.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>
<!--批量定义别名,别名就是该类首字母变为小写-->
<typeAliases>
<package name="com.ssm.pojo"/>
</typeAliases>
<mappers>
<mapper resource="com/ssm/dao/BooksMapper.xml"/>
</mappers>
</configuration>
5.编写service层的接口和实现类
public class BooksServiceImpl implements BooksService {
//调用dao层的操作,设置一个set接口,方便Spring管理
private BooksMapper booksMapper;
public void setBooksMapper(BooksMapper booksMapper){
this.booksMapper = booksMapper;
}
public int addBook(Books book) {
return booksMapper.addBook(book);
}
public int updateBook(Books book) {
return booksMapper.updateBook(book);
}
public int deleteBookByBookId(int bookId) {
return booksMapper.deleteBookByBookId(bookId);
}
public Books queryBookByBookId(int bookId) {
return booksMapper.queryBookByBookId(bookId);
}
public List<Books> queryBooks() {
return booksMapper.queryBooks();
}
}
5.spring层编写
1.编写spring整合mybatis的配置文件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"
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">
<!--关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!--获取连接池,此处用的是c3p0-->
<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}"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<!--绑定mybatis的配置文件-->
<property name="configLocation" value="classpath*:mybatis-config.xml"/>
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.ssm.dao"/>
</bean>
</beans>
2.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"
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">
<import resource="spring-dao.xml"/>
<!--1.扫描service层下的包-->
<context:component-scan base-package="com.ssm.service"/>
<!--2.将所有的业务类注入到spring中,可以通过配置,或者注解实现-->
<bean id="booksServiceImpl" class="com.ssm.service.BooksServiceImpl">
<!--如果ref="booksMapper"爆红,在该配置文件中关联dao层<import resource="spring-dao.xml"/>-->
<property name="booksMapper" ref="booksMapper"/>
</bean>
<!--3.声明事务式配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="datasource" />
</bean>
</beans>
6.springMVC层编写
1.配置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:spring-mvc.xml</param-value>
</init-param>
<!--和tomcat一起启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</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.配置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
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.注解驱动-->
<mvc:annotation-driven/>
<!--2.静态资源过滤-->
<mvc:default-servlet-handler/>
<!--3.上下文扫描包-->
<context:component-scan base-package="com.ssm.controller"/>
<!--4.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.spring配置整合文件application-context.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="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
</beans>
7.编写业务逻辑
@Controller
@RequestMapping("/book")
public class BooksController {
//controller层调用service层
@Autowired
@Qualifier("booksServiceImpl")
private BooksService booksService;
//查询全部书籍,并返回书籍展示页面
@RequestMapping("/displayBooks")
public String listBooks(Model model){
List<Books> books = booksService.queryBooks();
model.addAttribute("listBooks",books);
return "displayBooks";
}
//跳转到添加书籍的页面
@RequestMapping("/toAddBook")
public String toAddBook(){
return "addBookInfo";
}
//添加图书信息
@RequestMapping("/addBook")
public String addBook(Books book){
System.out.println(book);
booksService.addBook(book);
return "redirect:/book/displayBooks";
}
//删除图书信息
@RequestMapping("/deleteBook")
public String deleteBook(int bookId){
booksService.deleteBookByBookId(bookId);
return "redirect:/book/displayBooks";
}
//跳转到修改图书信息页面
@RequestMapping("/toUpdateBook")
public String toUpdateBook(int bookId,Model model){
Books book = booksService.queryBookByBookId(bookId);
model.addAttribute("book",book);
return "updateBook";
}
//修改图书信息
@RequestMapping("/updateBook")
public String updateBook(Books book){
booksService.updateBook(book);
return "redirect:/book/displayBooks";
}
//根据图书名搜索图书信息
@RequestMapping("/queryBooksByBookName")
public String queryBooksByBookName(String queryBookName,Model model){
System.out.println("进入controller中的queryBooksByBookName");
List<Books> books = booksService.queryBooksByBookName(queryBookName);
model.addAttribute("listBooks",books);
return "displayBooks";
}
}