Mybatis怎么与Spring结合使用-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

Mybatis怎么与Spring结合使用

Mybatis怎么与Spring结合使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

丹东网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。成都创新互联公司自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司

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

项目管理工具 : 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下)

<?xml version="1.0" encoding="UTF-8"?> 
 
   
   
    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下)

<?xml version="1.0" encoding="UTF-8"?> 
 
   
   
   
   
     
     
   
   
   
   
   
 

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目录下)

<?xml version="1.0" encoding="UTF-8"?> 
 
   
     
     
   
   
   
     
     
   
   
     
     
   
   
   
     
   
   
   
 

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目录下)

<?xml version="1.0" encoding="UTF-8" ?>  
 
 
   
   
   
   
   
   
    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结合使用
本文链接:http://kswsj.cn/article/gsggjp.html

其他资讯