Spring5整合MyBatis3

目录

 

Spring5整合MyBatis3

1.完成配置文件

2.持久层功能实现

3.服务层功能的实现

4.控制层功能实现

5.jsp页面

部署项目之后测试:


Spring5整合MyBatis3

 

这是源码地址:https://download.csdn.net/download/qq_41900081/10785290

先说一下整合过程中解决比较久的低级问题:

1.applicationContext.xml文件名写错

2.db.properties中内容写错,比如写成driver,url,username....正确为driverClass,jdbcUrl,user....

项目结构:

Spring5整合MyBatis3_第1张图片

user表

Spring5整合MyBatis3_第2张图片

book表

Spring5整合MyBatis3_第3张图片

1.完成配置文件

  db.properties

dataSource.driverClass=com.mysql.cj.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false&&serverTimezone=UTC
dataSource.user=root
dataSource.password=root
dataSource.maxPoolSize=20
dataSource.maxIdleTime = 1000
dataSource.minPoolSize=6
dataSource.initialPoolSize=5

applicationContext.xml



			      
	   
       
       
	 
    
    
	
	

	
	
	
	
	
	
	
	
	
	
	
	

springmvc-config.xml



        
    
    
    
    
    
    
   
      
    

 web.xml



	
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		contextConfigLocation
		/WEB-INF/applicationContext*.xml
	
	
	
  
    springmvc
    
        org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      /WEB-INF/springmvc-config.xml
    
    1
  
  
  
  
    springmvc
    /
  
  
  
  
		characterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
 
	
		characterEncodingFilter
		/*
	
	

 2.持久层功能实现

(两个普通的 javabean,写上几个属性和get/set方法)

User.java

package com.domain;

import java.io.Serializable;

public class User implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String loginname;
	private String password;
	private String username;
	private String phone;
	private String address;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public User() {
		super();
		
	}
	public User(Integer id, String loginname, String password, String username, String phone, String address) {
		super();
		this.id = id;
		this.loginname = loginname;
		this.password = password;
		this.username = username;
		this.phone = phone;
		this.address = address;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", loginname=" + loginname + ", password=" + password + ", username=" + username
				+ ", phone=" + phone + ", address=" + address + "]";
	}
}

Book.java

package com.domain;

import java.io.Serializable;
import java.util.Date;

public class Book implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private String author;
	private String publication; //出版社
	private Date publicationdate;//出版日期
	private Double price;
	private String image;
	private String remark;//描述
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getPublication() {
		return publication;
	}
	public void setPublication(String publication) {
		this.publication = publication;
	}
	public Date getPublicationdate() {
		return publicationdate;
	}
	public void setPublicationdate(Date publicationdate) {
		this.publicationdate = publicationdate;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public Book() {
		super();
		
	}
	@Override
	public String toString() {
		return "Book [id=" + id + ", name=" + name + ", author=" + author + ", publication=" + publication
				+ ", publicationdate=" + publicationdate + ", price=" + price + ", image=" + image + ", remark="
				+ remark + "]";
	}
	public Book(Integer id, String name, String author, String publication, Date publicationdate, Double price,
			String image, String remark) {
		super();
		this.id = id;
		this.name = name;
		this.author = author;
		this.publication = publication;
		this.publicationdate = publicationdate;
		this.price = price;
		this.image = image;
		this.remark = remark;
	}
	
}

BookMapper.java

package com.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Select;
import com.domain.Book;

/**
 * BookMapper接口
 * */
public interface BookMapper {

	/**
	 * 查询所有图书
	 * @return 图书对象集合
	 * */
	@Select(" select * from book ")
	List findAll();
	
}

UserMapper.java

package com.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.domain.User;

/**
 * UserMapper接口
 * */
public interface UserMapper {
	
	/**
	 * 根据登录名和密码查询用户
	 * @param String loginname
	 * @param String password
	 * @return 找到返回User对象,没有找到返回null
	 * */
	@Select("select * from user where loginname = #{loginname} and password = #{password}")
	User findWithLoginnameAndPassword(@Param("loginname")String loginname,
			@Param("password") String password);

}

3.服务层功能的实现

 BookService.java

package com.service;

import java.util.List;

import com.domain.Book;

public interface BookService {
	List getAll();
}

UserService.java

package com.service;

import com.domain.User;

public interface UserService {
	User login(String loginname, String password);
}

BookServiceImpl.java

package com.service.impl;

import java.util.List;
import com.domain.Book;
import com.mapper.BookMapper;
import com.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
@Service("bookService")

public class BookServiceImpl implements BookService{
	//自动注入BookMapper
	@Autowired
	private BookMapper bookMapper;
	@Transactional(readOnly=true)
	@Override
	public List getAll() {
		
		return bookMapper.findAll();
	}
	
}

UserSericeImpl.java

package com.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.domain.User;
import com.mapper.UserMapper;
import com.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService{
	//自动注入UserMapper 
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public User login(String loginname, String password) {
		return userMapper.findWithLoginnameAndPassword(loginname, password);
	}

}

4.控制层功能实现

FormController.java

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FormController {
	@RequestMapping("/login")
	public String login() {
		return "login";
	}
}

BookConroller.java

package com.controller;

import java.util.List;
import com.domain.Book;
import com.service.BookService;
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.RequestMapping;

/**
 * 处理图书请求控制器
 * */
@Controller
public class BookController {
	
	/**
	 * 自动注入BookService
	 * */
	@Autowired
	@Qualifier("bookService")
	private BookService bookService;

	/**
	 * 处理/main请求
	 * */
	@RequestMapping(value="/main")
	 public String main(Model model){
		// 获得所有图书集合
		List book_list = bookService.getAll();
		// 将图书集合添加到model当中
		model.addAttribute("book_list", book_list);
		// 跳转到main页面
		return "main";
	}
	
}

UserController.java

package com.controller;

import javax.servlet.http.HttpSession;
import com.domain.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

/**
 * 处理用户请求控制器
 * */
@Controller
public class UserController {
	
	/**
	 * 自动注入UserService
	 * */
	@Autowired
	@Qualifier("userService")
	private UserService userService;

	/**
	 * 处理/login请求
	 * */
	@PostMapping(value="/login")
	 public ModelAndView login(
			 String loginname,String password,
			 ModelAndView mv,
			 HttpSession session){
		// 根据登录名和密码查找用户,判断用户登录
		User user = userService.login(loginname, password);
		if(user != null){
			// 登录成功,将user对象设置到HttpSession作用范围域
			session.setAttribute("user", user);
			// 转发到main请求
			mv.setView(new RedirectView("main"));
		}else{
			// 登录失败,设置失败提示信息,并跳转到登录页面
			mv.addObject("message", "登录名或密码错误,请重新输入!");
			mv.setViewName("login");
		}
		return mv;
	}
	
	
}

5.jsp页面

login.jsp





<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

登录


登录页面

${requestScope.message}
登录名:
密码:

main.jsp





<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



首页


welcome ${sessionScope.user.username} login

封面 书名 作者 价格
${book.name } ${book.author } ${book.price}

部署项目之后测试:

Spring5整合MyBatis3_第4张图片

登录成功:

Spring5整合MyBatis3_第5张图片

登录失败:

Spring5整合MyBatis3_第6张图片

你可能感兴趣的:(web,框架)