mybatis实现图书管理类项目
sql要求
整个项目框架为:
整个工程项目从右往左依次建立,本次只基于mybatis,Spring 框架以后补充。
整个工程文件:
创建数据库mysql
mybatis-cfg.xml配置。
<?xml version="1.0" encoding="UTF-8" ?></mappers>
创建实体类entity。
package cn.kgc.kb07.entity;/*
*@
*@Date :
//
- @Description
实体User
*/
public class User {
private int user_id;
private String user_name;
private int grade;
private String phone;
private String user_type;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getUser_type() {
return user_type;
}
public void setUser_type(String user_type) {
this.user_type = user_type;
}
@Override
public String toString() {
return "User{" +
"user_id=" + user_id +
", user_name='" + user_name + '\'' +
", grade=" + grade +
", phone='" + phone + '\'' +
", user_type='" + user_type + '\'' +
'}';
}
}
/实体类Book同理/
创建DAO层。
/usermapper接口/
package cn.kgc.kb07.dao.user;/*
*@Author
*@Date :
*/
import cn.kgc.kb07.entity.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface UserMapper {//接口映射
public List getUserListByUserName(String userName);//#{userName}一致
public int add(User user);//user入参
}
/*/
/usermapper.xml映射文件
*/
select * from user
where user_name like CONCAT(’%’,#{user_name},’%’);
insert into user(user_name,grade,phone,user_type)
values (#{user_name},#{grade},#{phone},#{user_type})
//BookMapper和BookMapper.xml同理
创建serverce层。
/UserService/
package cn.kgc.kb07.service.user;/*
import cn.kgc.kb07.dao.user.UserMapper;
import cn.kgc.kb07.entity.User;
import cn.kgc.kb07.until.MapperConfig;
import org.apache.ibatis.session.SqlSession;
public class UserService {
public User getUser(String name){
MapperConfig m=new MapperConfig();
SqlSession session=m.getSession(true);
return (User) session.getMapper(UserMapper.class).getUserListByUserName(name);
}
}
//BookService同理
创建servlet层。
/UserServlet/
package cn.kgc.kb07.servlet;/*
*@Author
//
- @Description
*/
import cn.kgc.kb07.entity.Book;
import cn.kgc.kb07.entity.User;
import cn.kgc.kb07.service.book.BookService;
import cn.kgc.kb07.service.user.UserService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String name=req.getParameter("name");
UserService services=new UserService();
User user=services.getUser(name);
if(user!=null){
if(user.getUser_type().equals("1")){
req.getSession().setAttribute("user",user);
resp.sendRedirect("login1.jsp");
}else if(user.getUser_type().equals("2")){
req.getSession().setAttribute("user",user);
String book_name=req.getParameter("book_name");
String type_name=req.getParameter("type_name");
BookService service=new BookService();
List<Book> booksList= service.queryBooks(book_name,type_name);
req.getSession().setAttribute("book", booksList);
resp.sendRedirect("login2.jsp");
}else{
req.getSession().setAttribute("msg","输入不合法,重新输入");
resp.sendRedirect("index.jsp");
}
}else{
req.getSession().setAttribute("msg","输入不合法,重新输入");
resp.sendRedirect("index.jsp");
}
}
}
//BookServlet等其他类同理
创建until层。
/*
mapper配置,工厂设置,无需改动,可直接复制粘贴哦
*/package cn.kgc.kb07.until;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MapperConfig {
private static SqlSessionFactory factory;
public MapperConfig(){};
static{
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
try {
InputStream is= Resources.getResourceAsStream(“mybatis-cfg.xml”);
factory=builder.build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession(boolean autoCommit){
return factory.openSession(autoCommit);//默认为true关闭,false开启事务控制,
}
public static void closeSession(SqlSession session){
if(session!=null){
session.close();
}
}}
创建test文件夹。
/*
测试用的
*/
package cn.kgc.kb07.dao.user;
import cn.kgc.kb07.entity.User;
import cn.kgc.kb07.until.MapperConfig;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.apache.log4j.lf5.util.Resource;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import cn.kgc.kb07.until.MapperConfig.;
import static org.junit.Assert.;
/*
*@Author
*@Date :
*/public class UserMapperTest {
private Logger logger = Logger.getLogger(UserMapperTest.class);
@Test
public void getUserListByUserName() {
List<User> userList = null;
SqlSession sqlSession = null;
String userName="罗鑫";
sqlSession = MapperConfig.getSession(true);
// userList = sqlSession.selectList("cn.kgc.kb07.dao.user.UserMapper.xml.getUserList");
userList=sqlSession.getMapper(UserMapper.class).getUserListByUserName(userName);
for (User user : userList) {
logger.info("===========>getUserListByUserName\n用户:" + user.getUser_name()+
"\t用户电话:"+user.getPhone());
}
MapperConfig.closeSession(sqlSession);
}
@Test
public void add() throws ParseException {
logger.debug("----------------------->");
SqlSession session=null;
int count =0;
session=MapperConfig.getSession(true);
User user = new User();
user.setUser_name("李四");
user.setGrade(1);
user.setPhone("18860855751");
user.setUser_type("不知道");
//user.date(new SimpleDateFormat("yyyy-mm-dd").parse("2000-1-1"));
count=session.getMapper(UserMapper.class).add(user);
logger.info("插入"+count+"条数据");
MapperConfig.closeSession(session);
}}
//集中测试,其他层的方法都可在这创建测试类进行测试,可模仿上面
创建JSP页面显示。
/**/
<%–
Created by IntelliJ IDEA.
User: liyue
Date: 2020/6/15
Time: 22:53
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
欢迎登录
图书名图书类型编号 | 书名 | 价格 | 库存 | 介绍 | 类型名 |
---|---|---|---|---|---|
${user.book_id} | ${user.book_name} | ${user.price} | ${user.store} | ${user.des} | ${user.type_name} |
设置web.xml
/*User的登录界面
*/
login
cn.kgc.kb07.servlet.UserServlet
login
/login.do
##------------------部分成果,先睡了,以后继续完善-----------------------
登录
查找