Mybatis集成Spring的实例代码_动力节点Java 学院整理

所需要用到的其他工具或技术:

项目管理工具 : Maven

前台WEB展示:JSP

其他框架:Spring, Spring MVC

数据库 : Derby

新建一个Maven的Web项目

Maven Dependencies:

 
     
      org.springframework 
      spring-context 
      4.0.0.RELEASE 
     
     
      org.springframework 
      spring-webmvc 
      4.0.0.RELEASE 
     
     
      org.springframework 
      spring-tx 
      4.0.0.RELEASE 
     
     
      org.springframework 
      spring-jdbc 
      4.0.0.RELEASE 
     
     
     
      org.aspectj 
      aspectjrt 
      1.6.10 
     
     
     
      org.slf4j 
      slf4j-api 
      1.6.6 
     
     
      org.slf4j 
      jcl-over-slf4j 
      1.6.6 
      runtime 
     
     
      org.slf4j 
      slf4j-log4j12 
      1.6.6 
      runtime 
     
     
     
      javax.inject 
      javax.inject 
      1 
     
     
     
      javax.servlet 
      servlet-api 
      2.5 
      provided 
     
     
      javax.servlet.jsp 
      jsp-api 
      2.1 
      provided 
     
     
      javax.servlet 
      jstl 
      1.2 
     
     
     
      org.mybatis 
      mybatis 
      3.2.7 
     
     
      org.mybatis 
      mybatis-spring 
      1.2.1 
     
     
     
      junit 
      junit 
      4.9 
      test 
     
     
     
      org.apache.derby 
      derby 
      10.10.2.0 
     
     
      org.apache.derby 
      derbyclient 
      10.10.2.0 
     

SQL建表及数据插入

CREATE TABLE USER_TEST_TB(  
ID INT PRIMARY KEY,  
USERNAME VARCHAR(20) NOT NULL,  
PASSWORD VARCHAR(20) NOT NULL,  
NICKNAME VARCHAR(20) NOT NULL  
);    
INSERT INTO USER_TEST_TB VALUES(1,'1st','111','Jack');  
INSERT INTO USER_TEST_TB VALUES(2,'2nd','222','Rose');  
INSERT INTO USER_TEST_TB VALUES(3,'3rd','333','Will');  

web.xml(scr/main/webapp/WEB-INF下)

 
 
   
   
    contextConfigLocation 
    /WEB-INF/*Context.xml 
   
   
    org.springframework.web.context.ContextLoaderListener 
   
   
    appServlet 
    org.springframework.web.servlet.DispatcherServlet 
    1 
   
   
    appServlet 
    / 
   
 

appServlet-servlet.xml(Spring的Servlet配置文件scr/main/webapp/WEB-INF下)

 
 
   
   
   
   
     
     
   
   
   
   
   
 

JSP文件

show.jsp(src/main/webapp/WEB-INF/views目录下)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
  pageEncoding="ISO-8859-1"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
 
 
 
 
Show All Users 
 
 
 
   
        
USER_NAME PASSWORD NICK_NAME EDIT DELETE
${user.username} ${user.password} ${user.nickname} edit delete
Add new User

update.jsp(src/main/webapp/WEB-INF/views目录下)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
  pageEncoding="ISO-8859-1"%> 
 
 
 
 
Update Profile 
 
 
  
User ID:${user.id}
Username:
Password:
Nickname:

insert.jsp(src/main/webapp/WEB-INF/views目录下)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
  pageEncoding="ISO-8859-1"%> 
 
 
 
 
Insert Profile 
 
 
 
  
User Id:
Username:
Password:
Nickname:

applicationContext.xml(Spring的Application配置文件在src/main/resources目录下)

 
 
   
     
     
   
   
   
     
     
   
   
     
     
   
   
   
     
   
   
   
 

Java文件

UserController.Java(在src/main/java/com.bjpowernode.practice.controller目录下)

package com.bjpowernode.practice.controller; 
import com.bjpowernode.practice.User; 
import com.bjpowernode.practice.UserMapper; 
import java.util.List; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
@EnableWebMvc 
@Controller 
public class UserController 
{ 
  @Autowired 
  private UserMapper userMapper; 
  /** 
   * 
   * 获得所有的User信息 
   * 
   * @param model 
   * @return 
   */ 
  @RequestMapping(value = {"/", ""}, method = RequestMethod.GET) 
  public String getAllUser(Model model) 
  { 
    List users = userMapper.getUsers(); 
    System.out.println("Show all user size:" + users.size()); 
    model.addAttribute("users", users); 
    return "show"; 
  } 
  /** 
   * 
   * INSERT的GET请求,跳转到Insert的View即insert.jsp 
   * 
   * @return 
   */ 
  @RequestMapping(value = {"/insert", ""}, method = RequestMethod.GET) 
  public String insertUser() 
  { 
    return "insert"; 
  } 
  /** 
   * 
   * INSERT的POST请求,执行插入操作并返回ShowAll页面 
   * 
   * @param user 
   * @return 
   */ 
  @RequestMapping(value = {"/insert", ""}, method = RequestMethod.POST) 
  public String insertUserPOST(User user) 
  { 
    userMapper.insertUser(user); 
    return "redirect:/"; 
  } 
  /** 
   * 
   * UPDATE的GET请求,跳转到update的View即update.jsp 
   * 
   * @param id 
   * @param model 
   * @return 
   */ 
  @RequestMapping(value = {"/update/{id}", ""}, method = RequestMethod.GET) 
  public String updateUser(@PathVariable String id, Model model) 
  { 
    model.addAttribute("user", userMapper.getUser(Integer.valueOf(id))); 
    return "update"; 
  } 
  /** 
   * 
   * UPDATE的POST请求,执行更新操作并返回ShowAll页面 
   * 
   * @param id 
   * @param user 
   * @return 
   */ 
  @RequestMapping(value = {"/update/{id}", ""}, method = RequestMethod.POST) 
  public String updateUserPOST(@PathVariable String id, User user) 
  { 
    userMapper.updateUser(user); 
    return "redirect:/"; 
  } 
  /** 
   * 
   * 通过Id删除USER 
   * 
   * @param id 
   * @return 
   */ 
  @RequestMapping(value = {"/delete/{id}", ""}, method = RequestMethod.GET) 
  public String deleteUser(@PathVariable int id) 
  { 
    userMapper.deleteUser(id); 
    return "redirect:/"; 
  } 
} 

User.java(在src/main/java/com.bjpowernode.practice)

package com.bjpowernode.practice; 
/** 
 * 
 * User 对象。 
 * 
 * @author Freud Kang 
 * 
 */ 
public class User 
{ 
  private Integer id; 
  private String username; 
  private String password; 
  private String nickname; 
  public Integer getId() 
  { 
    return id; 
  } 
  public void setId(Integer id) 
  { 
    this.id = id; 
  } 
  public String getUsername() 
  { 
    return username; 
  } 
  public void setUsername(String username) 
  { 
    this.username = username; 
  } 
  public String getPassword() 
  { 
    return password; 
  } 
  public void setPassword(String password) 
  { 
    this.password = password; 
  } 
  public String getNickname() 
  { 
    return nickname; 
  } 
  public void setNickname(String nickname) 
  { 
    this.nickname = nickname; 
  } 
} 

UserMapper.java(在src/main/java/com.bjpowernode.practice目录下)

package com.bjpowernode.practice; 
import java.util.List; 
public interface UserMapper 
{ 
  /** 
   * 
   * 获得所有User 
   * 
   * @return 
   */ 
  public List getUsers(); 
  /** 
   * 
   * 通过Id获得User 
   * 
   * @param id 
   * @return 
   */ 
  public User getUser(int id); 
  /** 
   * 
   * 插入User 
   * 
   * @param user 
   */ 
  public void insertUser(User user); 
  /** 
   * 
   * 更新User 
   * 
   * @param user 
   */ 
  public void updateUser(User user); 
  /** 
   * 
   * 通过Id删除User 
   * 
   * @param userId 
   */ 
  public void deleteUser(int userId); 
} 

UserMapper.xml(mybatis的mapper配置文件,在src/main/java/com.bjpowernode.practice目录下)

  
 
 
   
   
   
   
   
   
    insert into  
      USER_TEST_TB  
    values(#{id},#{username},#{password},#{nickname}) 
   
   
   
    update USER_TEST_TB set 
      USERNAME = #{username}, 
      PASSWORD = #{password}, 
      NICKNAME = #{nickname} 
    where ID = #{id} 
   
   
   
    delete from USER_TEST_TB where ID=#{id} 
   
  

总结

以上所述是小编给大家介绍的Mybatis集成Spring的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

你可能感兴趣的:(Mybatis集成Spring的实例代码_动力节点Java 学院整理)