其它工具或技术需要使用:

项目管理工具 : Maven

前台WEB图库:JSP

其他框架:Spring, Spring MVC

数据库 : Derby

Maven的Web项目

Maven Dependencies:

<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency> <!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.10</version>
</dependency> <!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
<scope>runtime</scope>
</dependency> <!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency> <!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency> <!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency> <!-- Derby -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.10.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.10.2.0</version>
</dependency>

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"?

>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.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"> <!-- Spring 的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/*Context.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启Annotation支持 -->
<annotation-driven /> <!-- Spring的渲染层配置 -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean> <!-- Spring的Annotation默认扫描包 -->
<context:component-scan base-package="com.freud.practice" /> <!-- 引入其它Spring配置文件 -->
<beans:import resource="classpath:applicationContext.xml" /> </beans:beans>

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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Show All Users</title>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
</style>
</head>
<body> <table border="1px" bordercolor="green">
<thead>
<tr>
<th>USER_NAME</th>
<th>PASSWORD</th>
<th>NICK_NAME</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>
<c:forEach items="${users}" var="user" varStatus="status">
<tr>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.nickname}</td>
<td><a href="update/${user.id}">edit</a></td>
<td><a href="delete/${user.id}">delete</a></td>
</tr>
</c:forEach>
</thead>
</table>
<a href="insert">Add new User</a>
</body>
</html>

update.jsp(src/main/webapp/WEB-INF/views文件夹下)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update Profile</title>
</head>
<body>
<form action="${user.id}" method="post">
User ID:${user.id}<br>
Username:<input type="text" name="username" value="${user.username}"/><br>
Password:<input type="text" name="password" value="${user.password}"/><br>
Nickname:<input type="text" name="nickname" value="${user.nickname}"/><br>
<input type="submit" value="submit">
</form>
</body>
</html>

insert.jsp(src/main/webapp/WEB-INF/views文件夹下)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert Profile</title>
</head>
<body> <form action="" method="post">
User Id:<input type="text" name="id"><br>
Username:<input type="text" name="username" /><br>
Password:<input type="text" name="password"/><br>
Nickname:<input type="text" name="nickname"/><br>
<input type="submit" value="submit">
</form>
</body>
</html>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd"> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.freud.practice" />
<property name="sqlSessionFactoryBeanName" value="derbySqlSessionFactory" />
</bean> <!-- 配置Derby驱动数据源 -->
<bean id="derbyDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url" value="jdbc:derby://localhost:1527/freud;create=true" />
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
name="derbySqlSessionFactory">
<property name="dataSource" ref="derbyDataSource" />
<property name="mapperLocations" value="classpath*:com/freud/practice/*Mapper.xml" />
</bean> <!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="derbyDataSource" />
</bean> <!-- 开启基于注解的事务 -->
<tx:annotation-driven />
</beans>

Java文件

UserController.java(在src/main/java/com.freud.practice.controller文件夹下)

package com.freud.practice.controller;

import com.freud.practice.User;
import com.freud.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<User> 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.freud.practice)

package com.freud.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.freud.practice文件夹下)

package com.freud.practice;

import java.util.List;

public interface UserMapper
{ /**
*
* 获得全部User
*
* @return
*/
public List<User> 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.freud.practice文件夹下)

<?

xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.freud.practice.UserMapper"> <!-- 查询 -->
<select id="getUsers" resultType="com.freud.practice.User">
select *
from USER_TEST_TB
</select> <!-- 查询 -->
<select id="getUser" resultType="com.freud.practice.User">
select *
from USER_TEST_TB
where ID=#{id}
</select> <!-- 插入 -->
<insert id="insertUser">
insert into
USER_TEST_TB
values(#{id},#{username},#{password},#{nickname})
</insert> <!-- 更改 -->
<update id="updateUser">
update USER_TEST_TB set
USERNAME = #{username},
PASSWORD = #{password},
NICKNAME = #{nickname}
where ID = #{id}
</update> <!-- 删除 -->
<delete id="deleteUser">
delete from USER_TEST_TB where ID=#{id}
</delete>
</mapper>

版权声明:本文博主原创文章,博客,未经同意不得转载。

MyBatis系列教程(六)-- 与Spring综合(Integrate with Spring)的更多相关文章

  1. CRL快速开发框架系列教程六(分布式缓存解决方案)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  2. C#微信公众号开发系列教程六(被动回复与上传下载多媒体文件)

    微信公众号开发系列教程一(调试环境部署) 微信公众号开发系列教程一(调试环境部署续:vs远程调试) C#微信公众号开发系列教程二(新手接入指南) C#微信公众号开发系列教程三(消息体签名及加解密) C ...

  3. [转]Android Studio系列教程六--Gradle多渠道打包

    转自:http://www.stormzhang.com/devtools/2015/01/15/android-studio-tutorial6/ Android Studio系列教程六--Grad ...

  4. Android Studio系列教程六--Gradle多渠道打包

    Android Studio系列教程六--Gradle多渠道打包 2015 年 01 月 15 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://stormzh ...

  5. 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...

  6. webpack4 系列教程(六): 处理SCSS

    这节课讲解webpack4中处理scss.只需要在处理css的配置上增加编译scss的 LOADER 即可.了解更多处理css的内容 >>> >>> 本节课源码 & ...

  7. Spring Boot2 系列教程(六)自定义 Spring Boot 中的 starter

    我们使用 Spring Boot,基本上都是沉醉在它 Stater 的方便之中.Starter 为我们带来了众多的自动化配置,有了这些自动化配置,我们可以不费吹灰之力就能搭建一个生产级开发环境,有的小 ...

  8. 深入浅出Mybatis系列(六)---objectFactory、plugins、mappers简介与配置

    上篇文章<深入浅出Mybatis系列(五)---TypeHandler简介及配置(mybatis源码篇)>简单看了一下TypeHandler, 本次将结束对于mybatis的配置文件的学习 ...

  9. ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web API

    ASP.NET 5.0 的主要目标之一是统一MVC 和 Web API 框架应用. 接下来几篇文章中您会了解以下内容: ASP.NET MVC 6 中创建简单的web API. 如何从空的项目模板中启 ...

随机推荐

  1. 真实世界里的钢铁侠-特斯拉汽车创始人埃隆&#183;马斯克(Elon Musk)

    真实世界里的钢铁侠--特斯拉汽车公司和SpaceX公司总裁马斯克(31岁).当我们得意于「站在山上踢几块石头下去」或是「站在风口上的猪」的成功理论的时候,我们真的成功了吗?我们到底创造了什么?改变了什 ...

  2. hdu4487(概率dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4487 题意:开始位置在0,每一步可以向右向左或者不动,问走了n步后,路径中能到达最右的期望. 分析:d ...

  3. Objective-C之成魔之路【16-使用文件】

    郝萌主倾心贡献,尊重作者的劳动成果.请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠.支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 语言的设计 ...

  4. ACM-简单的主题Ignatius and the Princess II——hdu1027

    转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...

  5. Audio Offload

    Audio Offload 音频分载,是系统将音频分载到声卡硬件进行分载处理的功能.从Windows 8开始,音频的硬件加速和分载处理又回来了.为什么说又回来了呢? 因为声卡自创通公司发明开始,相当长 ...

  6. codeforces 112APetya and Strings(字符串水题)

    A. Petya and Strings 点击打开题目 time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. 原声JS瀑布流加延迟载入

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. java-IO操作性能对照

    在软件系统中.IO速度比内存速度慢,IO读写在非常多情况下会是系统的瓶颈. 在java标准IO操作中,InputStream和OutputStream提供基于流的IO操作.以字节为处理单位:Reade ...

  9. spring的长处 ioc aop

    spring 的长处? 1.减少了组件之间的耦合性 ,实现了软件各层之间的解耦 2.能够使用easy提供的众多服务.如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它非 ...

  10. Java并发编程(您不知道的线程池操作)

    Java并发编程(您不知道的线程池操作) 这几篇博客,一直在谈线程,设想一下这个场景,如果并发的线程很多,然而每个线程如果执行的时间很多的话,这样的话,就会大量的降低系统的效率.这时候就可以采用线程池 ...