Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
chenkunlia committed Mar 14, 2019
1 parent f3f90d7 commit 5501bd5
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 15 deletions.
37 changes: 37 additions & 0 deletions src/main/java/com/soecode/lyf/dao/FamilyDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.soecode.lyf.dao;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.soecode.lyf.entity.Book;

public interface FamilyDao {

/**
* 通过ID查询单本图书
*
* @param id
* @return
*/
Book queryById(long id);

/**
* 查询所有图书
*
* @param offset 查询起始位置
* @param limit 查询条数
* @return
*/
List<Book> queryAll(@Param("offset") int offset, @Param("limit") int limit);

/**
* 减少馆藏数量
*
* @param bookId
* @return 如果影响行数等于>1,表示更新的记录行数
*/
int reduceNumber(long familyId);

}
12 changes: 7 additions & 5 deletions src/main/java/com/soecode/lyf/dto/Result.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.soecode.lyf.dto;

import com.soecode.lyf.enums.ResultType;

/**
* 封装json对象,所有返回结果都使用它
*/
public class Result<T> {

private boolean success;// 是否成功标志
private ResultType success;// 是否成功标志

private T data;// 成功时返回的数据

Expand All @@ -15,22 +17,22 @@ public Result() {
}

// 成功时的构造器
public Result(boolean success, T data) {
public Result(ResultType success, T data) {
this.success = success;
this.data = data;
}

// 错误时的构造器
public Result(boolean success, String error) {
public Result(ResultType success, String error) {
this.success = success;
this.error = error;
}

public boolean isSuccess() {
public ResultType isSuccess() {
return success;
}

public void setSuccess(boolean success) {
public void setSuccess(ResultType success) {
this.success = success;
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/soecode/lyf/entity/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.soecode.lyf.entity;

import java.util.Date;

public class BaseEntity {
private long id;
private Date createDateTime;
private Date updateDateTime;
private String creator;
private String updator;

}
6 changes: 6 additions & 0 deletions src/main/java/com/soecode/lyf/entity/Family.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.soecode.lyf.entity;

public class Family {
private long id;

}
5 changes: 5 additions & 0 deletions src/main/java/com/soecode/lyf/enums/ResultType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.soecode.lyf.enums;

public enum ResultType {
SUCCESS,ERROR
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.soecode.lyf.listener;


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Enumeration;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.soecode.lyf.utils.JdbcUtils;


/**
* 关闭ComboPooledDataSource连接池监听器
* @author huangyong
* @date 2018年4月23日 下午8:54:34
*/
public class CloseDataSourceListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent arg0) {

}

@Override
public void contextDestroyed(ServletContextEvent arg0) {
JdbcUtils.closeDataSource();
//注销驱动
//得到所有注册了的驱动
Enumeration<Driver> drivers = DriverManager.getDrivers();
//遍历注销
while(drivers.hasMoreElements()){
try {
DriverManager.deregisterDriver(drivers.nextElement());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/soecode/lyf/utils/JdbcUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.soecode.lyf.utils;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;


/**
* 用Jdbc操作数据库的工具,本类依赖c3p0-config.xml文件
* @author huangyong
* @date 2018年4月8日 下午9:58:31
*
*/
public class JdbcUtils {
private static final ComboPooledDataSource ds = new ComboPooledDataSource();

/**
* 获得数据源
* @return DataSource 返回类型
*/
public static DataSource getDataSource(){
return ds;
}

/**
* 关闭数据源
* @return void 返回类型
*/
public static void closeDataSource(){
ds.close();
}
}
22 changes: 14 additions & 8 deletions src/main/java/com/soecode/lyf/web/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.soecode.lyf.dto.Result;
import com.soecode.lyf.entity.Book;
import com.soecode.lyf.enums.AppointStateEnum;
import com.soecode.lyf.enums.ResultType;
import com.soecode.lyf.exception.NoNumberException;
import com.soecode.lyf.exception.RepeatAppointException;
import com.soecode.lyf.service.BookService;
Expand All @@ -32,24 +33,29 @@ public class BookController {
private BookService bookService;

@RequestMapping(value = "/list", method = RequestMethod.GET)
private String list(Model model) {
@ResponseBody
private Result list(Model model) {
List<Book> list = bookService.getList();
model.addAttribute("list", list);
Result result=new Result();
result.setData(list);
result.setSuccess(ResultType.SUCCESS);
// list.jsp + model = ModelAndView
return "list";// WEB-INF/jsp/"list".jsp
return result;// WEB-INF/jsp/"list".jsp
}

@RequestMapping(value = "/{bookId}/detail", method = RequestMethod.GET)
private String detail(@PathVariable("bookId") Long bookId, Model model) {
@ResponseBody
private Book detail(@PathVariable("bookId") Long bookId, Model model) {
if (bookId == null) {
return "redirect:/book/list";
return null;
}
Book book = bookService.getById(bookId);
if (book == null) {
return "forward:/book/list";
return null;
}
model.addAttribute("book", book);
return "detail";
return book;
}

// ajax json
Expand All @@ -58,7 +64,7 @@ private String detail(@PathVariable("bookId") Long bookId, Model model) {
@ResponseBody
private Result<AppointExecution> appoint(@PathVariable("bookId") Long bookId, @RequestParam("studentId") Long studentId) {
if (studentId == null || studentId.equals("")) {
return new Result<>(false, "学号不能为空");
return new Result<>(ResultType.ERROR, "学号不能为空");
}
AppointExecution execution = null;
try {
Expand All @@ -70,7 +76,7 @@ private Result<AppointExecution> appoint(@PathVariable("bookId") Long bookId, @R
} catch (Exception e) {
execution = new AppointExecution(bookId, AppointStateEnum.INNER_ERROR);
}
return new Result<AppointExecution>(true, execution);
return new Result<AppointExecution>(ResultType.SUCCESS, execution);
}

}
4 changes: 2 additions & 2 deletions src/main/resources/jdbc.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:https://localhost:3307/ssm?useUnicode=true&characterEncoding=utf8
jdbc.url=jdbc:mysql:https://localhost:3306/chong_ni_test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
jdbc.username=root
jdbc.password=
jdbc.password=lyy@1551881752
38 changes: 38 additions & 0 deletions src/main/resources/mapper/FamilyDao.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http:https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.soecode.lyf.dao.FamilyDao">
<!-- 目的:为dao接口方法提供sql语句配置 -->
<select id="queryById" resultType="Family" parameterType="long">
<!-- 具体的sql -->
SELECT
book_id,
name,
number
FROM
book
WHERE
book_id = #{bookId}
</select>

<select id="queryAll" resultType="Family">
SELECT
book_id,
name,
number
FROM
book
ORDER BY
book_id
LIMIT #{offset}, #{limit}
</select>

<update id="reduceNumber">
UPDATE book
SET number = number - 1
WHERE
book_id = #{bookId}
AND number > 0
</update>
</mapper>
Binary file not shown.
4 changes: 4 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
version="3.1" metadata-complete="true">
<!-- 如果是用mvn命令生成的xml,需要修改servlet版本为3.1 -->
<!-- 配置DispatcherServlet -->

<listener>
<listener-class>com.soecode.lyf.listener.CloseDataSourceListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
Expand Down

0 comments on commit 5501bd5

Please sign in to comment.