10.Spring注解+MyBatis+Servlet
1.创建sql脚本
create table account
(
id int(10) unsigned not null auto_increment primary key,
account_number varchar(20) not null,
account_pwd varchar(10) not null,
account_money double(10,2) not null,
account_status int(5) not null,
create_time date not null
);
insert into account(account_number,account_pwd,
account_money,account_status,create_time) values
('','',3000.00,1,'2015-05-06'),
('','',6000.00,1,'2015-05-07'),
('','',4000.00,1,'2015-05-08'),
('','',5000.00,1,'2015-05-09'),
('','',7000.00,2,'2015-05-10'),
('','',9000.00,2,'2015-05-11');
commit;
select * from account; insert into account
(account_number,account_pwd,account_money,
account_status,create_time)
values(?,?,?,?,?)
account.sql
2.创建如下项目
3.导入jar文件
4.在src下的com.pojo包下创建Account.java
package com.pojo; public class Account {
private Integer id ;
private Integer account_status ;
private Double account_money ;
private String account_number ;
private String account_pwd ;
private String create_time ; public Account() {
}
public Account(Integer accountStatus, Double accountMoney,
String accountNumber, String accountPwd, String createTime) {
account_status = accountStatus;
account_money = accountMoney;
account_number = accountNumber;
account_pwd = accountPwd;
create_time = createTime;
}
public Account(Integer id, Integer accountStatus, Double accountMoney,
String accountNumber, String accountPwd, String createTime) {
this.id = id;
account_status = accountStatus;
account_money = accountMoney;
account_number = accountNumber;
account_pwd = accountPwd;
create_time = createTime;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAccount_status() {
return account_status;
}
public void setAccount_status(Integer accountStatus) {
account_status = accountStatus;
}
public Double getAccount_money() {
return account_money;
}
public void setAccount_money(Double accountMoney) {
account_money = accountMoney;
}
public String getAccount_number() {
return account_number;
}
public void setAccount_number(String accountNumber) {
account_number = accountNumber;
}
public String getAccount_pwd() {
return account_pwd;
}
public void setAccount_pwd(String accountPwd) {
account_pwd = accountPwd;
}
public String getCreate_time() {
return create_time;
}
public void setCreate_time(String createTime) {
create_time = createTime;
}
@Override
public String toString() {
return "Account [account_money=" + account_money + ", account_number="
+ account_number + ", account_pwd=" + account_pwd
+ ", account_status=" + account_status + ", create_time="
+ create_time + ", id=" + id + "]";
} }
Account.java
5.在src下的com.mapper包下创建AccountMapper.java
package com.mapper; import java.util.List; import com.pojo.Account; public interface AccountMapper {
List<Account> findAll(); }
AccountMapper.java
6.在src下的com.mapper包下创建AccountMapper.xml
<?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.mapper.AccountMapper"> <select id="findAll" resultType="com.pojo.Account">
select * from account
</select> </mapper>
AccountMapper.xml
7.在src下的com.mapper包下创建AccountMapper.xml
8.在src下的com.mapper.impl包下创建AccountMapperImpl.java
package com.mapper.impl; import java.util.List; import javax.annotation.Resource; import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository; import com.mapper.AccountMapper;
import com.pojo.Account; /**
*
* AccountMapperImpl accountMapperImpl=new AccountMapperImpl()
*
*/
@Repository
public class AccountMapperImpl implements AccountMapper {
//定义spring给mybatis提供的sqlsession对象的
@Resource
private SqlSessionTemplate sqlSessionTemplate; /**
* 查询所有
*/
public List<Account> findAll() {
AccountMapper mapper=sqlSessionTemplate.getMapper(AccountMapper.class);
return mapper.findAll();
} }
AccountMapperImpl.java
9.在src下的com.service包下创建AccountService.java
package com.service; import java.util.List; import com.pojo.Account; public interface AccountService {
List<Account> queryAccount(); }
AccountService.java
10.在src下的com.service.impl包下创建AccountServiceImpl.java
package com.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.mapper.AccountMapper;
import com.pojo.Account;
import com.service.AccountService;
@Service
public class AccountServiceImpl implements AccountService {
@Resource
private AccountMapper accountMapperImpl; public List<Account> queryAccount() { return accountMapperImpl.findAll();
} }
AccountServiceImpl.java
11.在src下的com.servlet包下创建AccountServlet.java
package com.servlet; import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.pojo.Account;
import com.service.impl.AccountServiceImpl;
import com.sun.org.apache.regexp.internal.RE; public class AccountServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.乱码处理
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//2.接受参数
String flag=request.getParameter("flag");
//3.业务处理
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); AccountServiceImpl serivce=(AccountServiceImpl) ac.getBean("accountServiceImpl"); if(flag==null){
List<Account> list=serivce.queryAccount();
request.setAttribute("list", list);
request.getRequestDispatcher("index.jsp").forward(request, response); }else if(flag.equals("findbyid")){ }else if(flag.equals("update")){ }else if(flag.equals("add")){ }else if(flag.equals("delete")){ }
//4.请求跳转
} }
AccountServlet.java
12.在WebRoot下的web.xml下配置
<?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">
<servlet>
<servlet-name>AccountServlet</servlet-name>
<servlet-class>com.servlet.AccountServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>AccountServlet</servlet-name>
<url-pattern>/AccountServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>AccountServlet</welcome-file>
</welcome-file-list>
</web-app>
web.xml
13.在src下创建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 全局注解参数 -->
<context:annotation-config/> <!--全局扫描包 -->
<context:component-scan base-package="com"/> <!-- 1.驱动管理数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/holly"/>
<property name="username" value="root"/>
<property name="password" value="ok"/>
</bean> <!-- 2.sqlsessionfatorybean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations">
<value>classpath:com/mapper/*.xml</value>
</property>
</bean> <!-- 3.sqlsessionTemplate -->
<!--SqlSessionTemplate sqlSessionTemplate=new SqlSessionTemplate(sqlSessionFactoryBean)-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactoryBean"/>
</bean> <!-- 4.AccountMapperImpl
<bean id="accountMapperImpl" class="com.mapper.impl.AccountMapperImpl">
accountMapperImpl.setSqlSessionTemplate(sqlSessionTemplate)
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean> 5.AccountServiceImpl
<bean id="accountServiceImpl" class="com.service.impl.AccountServiceImpl">
<property name="mapper" ref="accountMapperImpl"/>
</bean> --></beans>
applicationContext.xml
14.在WebRoot下编辑index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<h3><a href="save.jsp">添加</a></h3>
<table>
<tr>
<td>编号</td>
<td>卡号</td>
<td>余额</td>
<td>开户日期</td>
<td>状态</td>
<td>操作</td>
</tr>
<c:forEach var="i" items="${list}">
<tr>
<td>${i.id }</td>
<td>${i.account_number }</td>
<td>${i.account_money }</td>
<td>${i.create_time }</td>
<td>${i.account_status }</td>
<td><a href="AccountServlet?id=${i.id }&flag=findbyid">修改</a>
|
<a href="AccountServlet?id=${i.id }&flag=delete">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
index.jsp
15.运行
10.Spring注解+MyBatis+Servlet的更多相关文章
- Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...
- Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)
Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码) 备注: 之前在Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合中 ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十五)——SpringMVC注解开发(基础篇)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7065294.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十四)--S ...
- 详解spring boot mybatis全注解化
本文重点介绍spring boot mybatis 注解化的实例代码 1.pom.xml //引入mybatis <dependency> <groupId>org.mybat ...
- Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版
一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...
- Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用
Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...
- Spring boot Mybatis 整合(注解版)
之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...
- 在Servlet中获取Spring注解的bean
最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...
- 如约而至,Java 10 正式发布! Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势 努力的孩子运气不会太差,跌宕的人生定当更加精彩 优先队列详解(转载)
如约而至,Java 10 正式发布! 3 月 20 日,Oracle 宣布 Java 10 正式发布. 官方已提供下载:http://www.oracle.com/technetwork/java ...
随机推荐
- 查看scn headroom变化趋势的几种方法
查看scn headroom变化趋势的几种方法 scn headroom问题,本文不做解释. 本文为自己的总结,脚本来自于oracle sr技术project师. 转载请注明出处http://blog ...
- 如何将unity资源窗体中的文件一下所有折叠/打开
1.选中父物体 2.按住alt 3.再按下键盘上的左键/右键:此父物体下的所有折叠/打开 或者 alt + LMB 点击所要折叠/打开的父物体左边的小三角
- Advapi32.dll 函数接口说明
Advapi32.dll 函数接口说明 函数原型 说明 AbortSystemShutDown ...
- <LeetCode OJ> 226. Invert Binary Tree
226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...
- Rockchip平台TP驱动详解【转】
本文转载自:http://blog.csdn.net/encourage2011/article/details/51679332 本文描述在RK3126平台上添加一个新的TP驱动(gslx680驱动 ...
- CodeForces - 735D Taxes (哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- codeforces 782B The Meeting Place Cannot Be Changed+hdu 4355+hdu 2438 (三分)
B. The Meeting Place Cannot Be Change ...
- JFreeChart之堆叠柱形图(StackedBar)
JFreeChart之堆叠柱形图(StackedBar) JAVA JFreeChart 最近的项目使用有个功能需要使用到堆叠柱形图,看了项目以前的代码实现没有想要的结果.所以自己就先到官网下载了 D ...
- javascript必须知道的知识要点(二)
该文章不详细叙述各知识要点的具体内容,仅把要点列出来,供大家学习的时候参照,或者检测自己是否熟练掌握了javascript,清楚各个部分的内容. 内建对象可划分为数据封装类对象.工具类对象.错误类对象 ...
- Blue Jeans(串)
http://poj.org/problem?id=3080 寻找最长公共子串..暴搜的.. #include<stdio.h> #include<string.h> int ...