Flask接口签名sign原理与实例代码浅析
287
2023-04-10
Mybatis集成Spring的实例代码_动力节点Java 学院整理
所需要用到的其他工具或技术:
项目管理工具 : Maven
前台WEB展示:jsP
其他框架:Spring, Spring MVC
数据库 : Derby
新建一个Maven的Web项目
Maven Dependencies:
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下)
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
appServlet-servlet.xml(Spring的Servlet配置文件scr/main/webapp/WEB-INF下)
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:beans="http://springframework.org/schema/beans" xmlns:context="http://springframework.org/schema/context" xsi:schemaLocation="http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:beans="http://springframework.org/schema/beans"
xmlns:context="http://springframework.org/schema/context"
xsi:schemaLocation="http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd
http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">
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" %>
USER_NAME PASSWORD NICK_NAME EDIT DELETE ${user.username} ${user.password} ${user.nickname} edit delete
update.jsp(src/main/webapp/WEB-INF/views目录下)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
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"%>
User Id:
Username:
Password:
Nickname:
applicationContext.xml(Spring的Application配置文件在src/main/resources目录下)
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop" xmlns:context="http://springframework.org/schema/context" xmlns:lang="http://springframework.org/schema/lang" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:tx="http://springframework.org/schema/tx" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd http://springframework.org/schema/lang http://springframework.org/schema/lang/spring-lang-4.0.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd"> class="org.springframework.jdbc.datasource.DriverManagerDataSource"> name="derbySqlSessionFactory">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"
xmlns:context="http://springframework.org/schema/context"
xmlns:lang="http://springframework.org/schema/lang" xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd
http://springframework.org/schema/lang http://springframework.org/schema/lang/spring-lang-4.0.xsd
http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd">
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
name="derbySqlSessionFactory">
name="derbySqlSessionFactory">
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
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
/**
*
* 通过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目录下)
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select *
from USER_TEST_TB
select *
from USER_TEST_TB
where ID=#{id}
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的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~