<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13version>
dependency>
<dependency>
<groupId>com.oraclegroupId>
<artifactId>ojdbc6artifactId>
<version>12.1.0.1-atlassian-hostedversion>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.5version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.4version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.4version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.10.2version>
dependency>
dependencies>
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
settings>
<typeAliases>
<package name="com.aaa.pojo"/>
typeAliases>
configuration>
<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
">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
bean>
<bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:com/aaa/dao/**/*.xml"/>
<property name="configLocation" value="classpath:mybaits-config.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactoryBean"/>
<property name="basePackage" value="com.aaa.dao"/>
bean>
beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<context:scan base-package="com.aaa.service"/>
<bean id="booksService" class="com.aaa.service.BooksServiceImpl">
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
beans>
<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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>
<context:component-scan base-package="com.aaa.controller"/>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="utf-8"/>
bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
bean>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
beans>
<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>
<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">
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>encodingfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
至此:ssm的配置就完成了
package com.aaa.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
public class Books {
private int BookId;
private String BookName;
private int BookCount;
private String Detail;
public Books(int bookId, String bookName) {
BookId = bookId;
BookName = bookName;
}
}
package com.aaa.pojo;
import java.io.Serializable;
public class Test implements Serializable {
private Integer code;//这是是响应码
private Object data;
public Test() {
}
public Test(Integer code) {
this.code = code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
package com.aaa.dao;
import com.aaa.pojo.Books;
import com.aaa.pojo.Test;
import java.util.List;
import java.util.Map;
public interface BooksMapper {
//查询全部
List<Books> queryAll();
//根据条件进行查询
List<Books> queryOnes(int BookId); //因为layui中要解析的数据格式不一样,所以使用集合
//增加
int addBooks(Books books);
//删除
int deleteBooks(int BookId);
//修改
int updateBooks(Books books);
}
<mapper namespace="com.aaa.dao.BooksMapper">
<select id="queryAll" resultType="books">
select * from BOOKS order by BOOKID asc
select>
<select id="queryOnes" resultType="books">
select * from BOOKS
<where>
BOOKID=#{BookId}
where>
select>
<insert id="addBooks" parameterType="books">
insert into BOOKS (BOOKID,BOOKNAME,BOOKCOUNT,DETAIL)
values(#{BookId},#{BookName},#{BookCount},#{Detail})
insert>
<update id="updateBooks" parameterType="books">
update BOOKS set BOOKNAME=#{BookName} where BOOKID=#{BookId}
update>
<delete id="deleteBooks" parameterType="_int">
delete from BOOKS where BOOKID=#{BookId}
delete>
mapper>
package com.aaa.service;
import com.aaa.pojo.Books;
import com.aaa.pojo.Test;
import java.util.List;
import java.util.Map;
public interface BooksService {
//查询全部
List<Books> queryAll();
//根据条件进行查询
List<Books> queryOnes(int BookId);
//增加
int addBooks(Books books);
//删除
int deleteBooks(int BookId);
//修改
int updateBooks(Books books);
}
package com.aaa.service;
import com.aaa.dao.BooksMapper;
import com.aaa.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class BooksServiceImpl implements BooksService {
@Autowired
private BooksMapper booksMapper;
public List<Books> queryOnes(int BookId) {
return booksMapper.queryOnes(BookId);
}
public List<Books> queryAll() {
return booksMapper.queryAll();
}
public int addBooks(Books books) {
return booksMapper.addBooks(books);
}
public int deleteBooks(int BookId) {
return booksMapper.deleteBooks(BookId);
}
public int updateBooks(Books books) {
return booksMapper.updateBooks(books);
}
}
2.4编写控制器层
package com.aaa.controller;
import com.aaa.pojo.Books;
import com.aaa.pojo.Test;
import com.aaa.service.BooksService;
import com.aaa.until.LayUiCode;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.omg.PortableInterceptor.INACTIVE;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/u1")
public class MyController {
@Autowired
@Qualifier("booksService")
private BooksService booksService;
//跳转路径
@RequestMapping("/test")
public String test(Model model){
List<Books> list = booksService.queryAll();
model.addAttribute("list",list);
return "queryALL";
}
//查询全部书籍
@GetMapping("/queryAll")
@ResponseBody
public String queryAll() throws JsonProcessingException {
List<Books> list = booksService.queryAll();
Test test = LayUiCode.LayCode(list);
ObjectMapper mapper=new ObjectMapper();
String s = mapper.writeValueAsString(test);
return s;
}
//查询单个书籍
@GetMapping("/queryAll2")
@ResponseBody
public String queryAlls(int bookIds) throws JsonProcessingException {
List<Books> list = booksService.queryOnes(bookIds);
Test test = LayUiCode.LayCode(list);
ObjectMapper mapper=new ObjectMapper();
String s = mapper.writeValueAsString(test);
return s;
}
//增加书籍
@PostMapping("/addBooks")
@ResponseBody
public String addBooks(HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {
String bian = request.getParameter("bian");
int i= Integer.parseInt(bian);
String ming = request.getParameter("ming");
int count= Integer.parseInt(request.getParameter("count"));
String miaoshu = request.getParameter("miaoshu");
Books books=new Books(i,ming,count,miaoshu);
int i1 = booksService.addBooks(books);
String s = JSON.toJSONString(books);
return s;
}
//删除书籍
@PostMapping("/deleteBooks")
@ResponseBody
public String deleteBooks(int bookId) throws JsonProcessingException {
int i = booksService.deleteBooks(bookId);
ObjectMapper mapper=new ObjectMapper();
String s = mapper.writeValueAsString(i);
return s;
}
//修改书籍
@PostMapping("/updateBooks")
@ResponseBody
public String updateBooks(int bookId,String bookName) throws JsonProcessingException {
Books books=new Books(bookId,bookName);
int i = booksService.updateBooks(books);
ObjectMapper mapper=new ObjectMapper();
String s = mapper.writeValueAsString(i);
System.out.println("测试值是"+i);
System.out.println("得到的值是"+bookName);
return s;
}
}
(这里只是封装一下layui的响应格式)预防前端报错
package com.aaa.until;
import com.aaa.pojo.Test;
import java.util.List;
public class LayUiCode {
public static Test LayCode(Object object){
Test test=new Test(200);
test.setData(object);
return test;
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
跳转页面
跳转页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
查询所有的书籍
<%--增加的页面层---%>
<%--修改的页面层--%>
注意:因为queryALL.jsp在WEB-INF下jsp这个包中,所以我们直接访问queryALL.jsp访问不到,所以我们要进行跳转