今天来搭建一个简单的SSM项目~
不必多说介绍话,直接上代码!不!上xml!
1.引入相应的jar
2.创建数据库
CREATE DATABASE books;
USE books;
CREATE TABLE book_info(
book_id INT(10) PRIMARY KEY,
book_code VARCHAR(50) NOT NULL,
book_name VARCHAR(50) NOT NULL,
book_type INT(10) NOT NULL,
book_author VARCHAR(20) NOT NULL,
publish_press VARCHAR(200) NOT NULL,
publish_date DATE NOT NULL,
is_borrow INT NOT NULL,
createdBy VARCHAR(50) NOT NULL,
creation_time DATE NOT NULL,
last_updatetime DATE NOT NULL
)
CREATE TABLE book_type(
id INT(10) PRIMARY KEY,
type_name VARCHAR(20) NOT NULL
);
3.使用mybatis逆向工程生成实体
请去参照我之前的博文:https://blog.csdn.net/CHENGHAO1473690852/article/details/92087071
4.创建mapper.java 、mapper.xml
package cn.chenghao.mapper;
import cn.chenghao.beans.Book_Info;
import cn.chenghao.beans.Book_Type;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
/**
* 查询所有信息
* @return
*/
List<Book_Info> selectBookAll();
/**
* 查询所有分类
* @return
*/
List<Book_Type> selectTypeAll();
/**
* 按照条件查询信息
* @return
*/
List<Book_Info> selectBookWhere(@Param("book_Name") Object book_Name, @Param("type_Name")Object type_Name, @Param("is_Borrow")Object is_Borrow);
}
<?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="cn.chenghao.mapper.BookMapper">
<resultMap type="Book_Info" id="mySel">
<result column="book_Code" jdbcType="VARCHAR" property="bookCode" />
<result column="book_Name" jdbcType="VARCHAR" property="bookName" />
<result column="book_Type" jdbcType="INTEGER" property="bookType" />
<result column="book_Author" jdbcType="VARCHAR" property="bookAuthor" />
<result column="publish_Press" jdbcType="VARCHAR" property="publishPress" />
<result column="is_Borrow" jdbcType="INTEGER" property="isBorrow" />
<association property="bookType" javaType="Book_Type">
<result column="type_Name" jdbcType="VARCHAR" property="typeName" />
</association>
</resultMap>
<select id="selectBookAll" resultMap="mySel">
SELECT
book_code,type_Name as book_TypeName,book_name,book_author,publish_press,is_borrow
FROM book_info,book_type
WHERE book_info.book_type=book_type.id
</select>
<select id="selectTypeAll" resultType="Book_Type">
select id,type_name from book_type
</select>
<select id="selectBookWhere" resultMap="mySel">
SELECT
book_code,type_Name as book_TypeName,book_name,book_author,publish_press,is_borrow
FROM book_info,book_type
WHERE book_info.book_type=book_type.id
<if test="book_Name != null and book_Name != ''">
and book_Name=#{book_Name}
</if>
<if test="type_Name != -1">
and book_Type=#{type_Name}
</if>
<if test="is_Borrow != -1">
and is_Borrow=#{is_Borrow}
</if>
</select>
</mapper>
5.创建Service、ServiceImpl
Service省略。。。。。与mapper.java相同!!!
package cn.chenghao.service.impl;
import cn.chenghao.beans.Book_Info;
import cn.chenghao.beans.Book_Type;
import cn.chenghao.mapper.BookMapper;
import cn.chenghao.service.BookService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;//自动注入mapper
@Override
public List<Book_Info> selectBookAll() {
return bookMapper.selectBookAll();
}
@Override
public List<Book_Type> selectTypeAll() {
return bookMapper.selectTypeAll();
}
@Override
public List<Book_Info> selectBookWhere(Object book_Name, Object type_Name, Object is_Borrow) {
return bookMapper.selectBookWhere(book_Name, type_Name, is_Borrow);
}
}
6.创建Controller
package cn.chenghao.controller;
/*
* @Author chenghao
* @Date 上午 11:08 2019-06-16
* Controller
*/
import cn.chenghao.beans.Book_Info;
import cn.chenghao.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/book/")
public class BookController {
@Resource(name = "bookServiceImpl")
private BookService bookService;
/**
* 查询所有信息
*
* @return
*/
@RequestMapping("selectBookAll")
public ModelAndView selectBookAll(HttpSession session, @RequestParam(required = true,defaultValue = "1") Integer page) {
ModelAndView modelAndView = new ModelAndView();
PageHelper.startPage(page,3);//配置插件分页,每页显示三条数据
List<Book_Info> selectBook = bookService.selectBookAll();//获取数据
PageInfo<Book_Info> book_infoPageInfo = new PageInfo<Book_Info>(selectBook);//声明PageInfo,放入参数
modelAndView.setViewName("bookInfo");
session.setAttribute("type", bookService.selectTypeAll());
modelAndView.addObject("page",book_infoPageInfo);
modelAndView.addObject("selectBook", selectBook);
return modelAndView;
}
/**
* 按照条件查询信息
*
* @param request
* @return
*/
@RequestMapping(value = "selectBookWhere",method = RequestMethod.POST)
public ModelAndView selectBookWhere(HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView();
//获取值
Integer type_Name = Integer.parseInt(request.getParameter("bookType"));
String book_Name = request.getParameter("book_Name");
Integer is_Borrow = Integer.parseInt(request.getParameter("BookIs"));
modelAndView.addObject("selectBook", bookService.selectBookWhere(book_Name, type_Name, is_Borrow));
modelAndView.setViewName("bookInfo");
return modelAndView;
}
}
7.创建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>
<!--驼峰命名-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 全局映射器启用缓存 -->
<setting name="cacheEnabled" value="true"/>
<!-- 查询时,关闭关联对象即时加载以提高性能 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指定),不会加载关联表的所有字段,以提高性能 -->
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!-- 允许使用列标签代替列名 -->
<setting name="useColumnLabel" value="true"/>
<!-- 允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 给予被嵌套的resultMap以字段-属性的映射支持 -->
<setting name="autoMappingBehavior" value="FULL"/>
<!-- 对于批量更新操作缓存SQL以提高性能 -->
<setting name="defaultExecutorType" value="BATCH"/>
<!-- 数据库超过25000秒仍未响应则超时 -->
<setting name="defaultStatementTimeout" value="25000"/>
</settings>
<typeAliases>
<package name="cn.chenghao.beans"/>
</typeAliases>
<!--配置拦截器插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
</configuration>
8.创建applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!--扫描包-->
<context:component-scan base-package="cn.chenghao"/>
<!--引入properties文件-->
<context:property-placeholder location="classpath:db.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>
<!--配置mybatis-spring整合-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定数据源-->
<property name="dataSource" ref="dataSource"/>
<!--指定mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
</bean>
<!--配置扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描所有mapper接口,加入ioc容器中-->
<property name="basePackage" value="cn.chenghao.mapper"/>
</bean>
</beans>
9.创建springmvc
<?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">
<!--启用springmvc注解-->
<context:annotation-config/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--扫描包-->
<context:component-scan base-package="cn.chenghao.controller"/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
10.配置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">
<!--指定spring主配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--字符编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--springmvc中央调度器-->
<servlet>
<servlet-name>MyDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!--Tomcat启动时,优先创建-->
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>MyDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
11.创建bookInfo.jsp
<%--
Created by IntelliJ IDEA.
User: chenghao
Date: 2019-06-16
Time: 上午 11:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>信息</title>
</head>
<body>
<h2>图书借阅系统</h2>
<div>
<form action="selectBookWhere" method="post">
图书分类:
<select name="bookType">
<option value="-1">--请选择--</option>
<c:forEach items="${sessionScope.type}" var="t">
<option value="${t.id}">${t.typeName}</option>
</c:forEach>
</select>
图书名称:
<input type="text" name="book_Name"/>
是否借阅:
<select name="BookIs">
<option value="-1">--请选择--</option>
<option value="0">未借阅</option>
<option value="1">已借阅</option>
</select>
<button>查询</button>
</form>
</div>
<table>
<tr>
<th>图书编号</th>
<th>图书分类</th>
<th>图书名称</th>
<th>作者</th>
<th>出版社</th>
<th>状态</th>
</tr>
<c:forEach items="${selectBook}" var="sel">
<tr>
<td>${sel.bookCode}</td>
<td>${sel.bookTypeName}</td>
<td>${sel.bookName}</td>
<td>${sel.bookAuthor}</td>
<td>${sel.publishPress}</td>
<td>
<c:if test="${sel.isBorrow == 0}">申请借阅</c:if>
<c:if test="${sel.isBorrow == 1}">已借阅</c:if>
</td>
</tr>
</c:forEach>
</table>
<div>
<P>
总${page.pages}页
</P>
<a href="selectBookAll?page=${page.firstPage}">首页</a>
<a href="selectBookAll?page=${page.prePage}">上一页</a>
<a href="selectBookAll?page=${page.nextPage}">下一页</a>
<a href="selectBookAll?page=${page.lastPage}">尾页</a>
</div>
</body>
</html>
12.运行测试