zoukankan      html  css  js  c++  java
  • 10.Spring注解+MyBatis+Servlet

    1.创建sql脚本

     1 create table account
     2 (
     3   id int(10) unsigned not null auto_increment primary key,
     4   account_number varchar(20) not null,
     5   account_pwd varchar(10) not null,
     6   account_money double(10,2) not null,
     7   account_status int(5) not null,
     8   create_time date not null
     9 );
    10 insert into account(account_number,account_pwd,
    11 account_money,account_status,create_time) values
    12 ('32561589654','123',3000.00,1,'2015-05-06'),
    13 ('12345678910','123',6000.00,1,'2015-05-07'),
    14 ('32561588568','123',4000.00,1,'2015-05-08'),
    15 ('32561587596','123',5000.00,1,'2015-05-09'),
    16 ('32561581235','123',7000.00,2,'2015-05-10'),
    17 ('32561581459','123',9000.00,2,'2015-05-11');
    18 commit;
    19 select * from account;
    20 
    21 insert into account
    22 (account_number,account_pwd,account_money,
    23 account_status,create_time) 
    24 values(?,?,?,?,?)
    account.sql

    2.创建如下项目

    3.导入jar文件

    4.在src下的com.pojo包下创建Account.java

     1 package com.pojo;
     2 
     3 public class Account {
     4     private Integer id             ;
     5     private Integer account_status ;
     6     private Double account_money  ;
     7     private String account_number ;
     8     private String account_pwd    ;
     9     private String create_time    ;
    10     
    11     
    12     public Account() {
    13     }
    14     public Account(Integer accountStatus, Double accountMoney,
    15             String accountNumber, String accountPwd, String createTime) {
    16         account_status = accountStatus;
    17         account_money = accountMoney;
    18         account_number = accountNumber;
    19         account_pwd = accountPwd;
    20         create_time = createTime;
    21     }
    22     public Account(Integer id, Integer accountStatus, Double accountMoney,
    23             String accountNumber, String accountPwd, String createTime) {
    24         this.id = id;
    25         account_status = accountStatus;
    26         account_money = accountMoney;
    27         account_number = accountNumber;
    28         account_pwd = accountPwd;
    29         create_time = createTime;
    30     }
    31     public Integer getId() {
    32         return id;
    33     }
    34     public void setId(Integer id) {
    35         this.id = id;
    36     }
    37     public Integer getAccount_status() {
    38         return account_status;
    39     }
    40     public void setAccount_status(Integer accountStatus) {
    41         account_status = accountStatus;
    42     }
    43     public Double getAccount_money() {
    44         return account_money;
    45     }
    46     public void setAccount_money(Double accountMoney) {
    47         account_money = accountMoney;
    48     }
    49     public String getAccount_number() {
    50         return account_number;
    51     }
    52     public void setAccount_number(String accountNumber) {
    53         account_number = accountNumber;
    54     }
    55     public String getAccount_pwd() {
    56         return account_pwd;
    57     }
    58     public void setAccount_pwd(String accountPwd) {
    59         account_pwd = accountPwd;
    60     }
    61     public String getCreate_time() {
    62         return create_time;
    63     }
    64     public void setCreate_time(String createTime) {
    65         create_time = createTime;
    66     }
    67     @Override
    68     public String toString() {
    69         return "Account [account_money=" + account_money + ", account_number="
    70                 + account_number + ", account_pwd=" + account_pwd
    71                 + ", account_status=" + account_status + ", create_time="
    72                 + create_time + ", id=" + id + "]";
    73     }
    74     
    75     
    76 }
    Account.java

    5.在src下的com.mapper包下创建AccountMapper.java

     1 package com.mapper;
     2 
     3 import java.util.List;
     4 
     5 import com.pojo.Account;
     6 
     7 public interface AccountMapper {
     8     List<Account> findAll();
     9 
    10 }
    AccountMapper.java

    6.在src下的com.mapper包下创建AccountMapper.xml

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    3 <mapper namespace="com.mapper.AccountMapper">
    4 
    5   <select id="findAll" resultType="com.pojo.Account">
    6      select * from  account
    7   </select>
    8   
    9 </mapper>
    AccountMapper.xml

    7.在src下的com.mapper包下创建AccountMapper.xml

    8.在src下的com.mapper.impl包下创建AccountMapperImpl.java

     1 package com.mapper.impl;
     2 
     3 import java.util.List;
     4 
     5 import javax.annotation.Resource;
     6 
     7 import org.mybatis.spring.SqlSessionTemplate;
     8 import org.springframework.stereotype.Repository;
     9 
    10 import com.mapper.AccountMapper;
    11 import com.pojo.Account;
    12 
    13 /**
    14  * 
    15  * AccountMapperImpl accountMapperImpl=new AccountMapperImpl()
    16  *
    17  */
    18 @Repository
    19 public class AccountMapperImpl implements AccountMapper {
    20     //定义spring给mybatis提供的sqlsession对象的
    21     @Resource
    22     private SqlSessionTemplate sqlSessionTemplate;
    23    
    24    /**
    25     * 查询所有
    26     */
    27     public List<Account> findAll() {
    28         AccountMapper mapper=sqlSessionTemplate.getMapper(AccountMapper.class);
    29         return mapper.findAll();
    30     }
    31 
    32     
    33     
    34 }
    AccountMapperImpl.java

    9.在src下的com.service包下创建AccountService.java

     1 package com.service;
     2 
     3 import java.util.List;
     4 
     5 import com.pojo.Account;
     6 
     7 public interface AccountService {
     8     List<Account> queryAccount();
     9 
    10 }
    AccountService.java

    10.在src下的com.service.impl包下创建AccountServiceImpl.java

     1 package com.service.impl;
     2 
     3 import java.util.List;
     4 
     5 import javax.annotation.Resource;
     6 
     7 import org.springframework.stereotype.Service;
     8 
     9 import com.mapper.AccountMapper;
    10 import com.pojo.Account;
    11 import com.service.AccountService;
    12 @Service
    13 public class AccountServiceImpl implements AccountService {
    14     @Resource
    15     private AccountMapper accountMapperImpl;
    16     
    17     
    18     public List<Account> queryAccount() {
    19         
    20         return accountMapperImpl.findAll();
    21     }
    22 
    23 }
    AccountServiceImpl.java

    11.在src下的com.servlet包下创建AccountServlet.java

     1 package com.servlet;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 import java.util.List;
     6 
     7 import javax.servlet.ServletException;
     8 import javax.servlet.http.HttpServlet;
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 
    12 import org.springframework.context.ApplicationContext;
    13 import org.springframework.context.support.ClassPathXmlApplicationContext;
    14 
    15 import com.pojo.Account;
    16 import com.service.impl.AccountServiceImpl;
    17 import com.sun.org.apache.regexp.internal.RE;
    18 
    19 public class AccountServlet extends HttpServlet {
    20 
    21     public void doGet(HttpServletRequest request, HttpServletResponse response)
    22             throws ServletException, IOException {
    23        this.doPost(request, response);
    24     }
    25 
    26 
    27     public void doPost(HttpServletRequest request, HttpServletResponse response)
    28             throws ServletException, IOException {
    29         //1.乱码处理
    30         request.setCharacterEncoding("UTF-8");
    31         response.setCharacterEncoding("UTF-8");
    32         response.setContentType("text/html;charset=UTF-8");
    33         //2.接受参数
    34         String flag=request.getParameter("flag");
    35         //3.业务处理
    36         ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    37         
    38         
    39         
    40         AccountServiceImpl serivce=(AccountServiceImpl) ac.getBean("accountServiceImpl");
    41         
    42         if(flag==null){
    43             List<Account> list=serivce.queryAccount();
    44             request.setAttribute("list", list);
    45             request.getRequestDispatcher("index.jsp").forward(request, response);
    46         
    47         }else if(flag.equals("findbyid")){
    48             
    49         }else if(flag.equals("update")){
    50             
    51         }else if(flag.equals("add")){
    52             
    53         }else if(flag.equals("delete")){
    54             
    55         }
    56         //4.请求跳转
    57     }
    58 
    59 }
    AccountServlet.java

    12.在WebRoot下的web.xml下配置

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app version="2.5" 
     3     xmlns="http://java.sun.com/xml/ns/javaee" 
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     7   <servlet>
     8     <servlet-name>AccountServlet</servlet-name>
     9     <servlet-class>com.servlet.AccountServlet</servlet-class>
    10   </servlet>
    11 
    12   <servlet-mapping>
    13     <servlet-name>AccountServlet</servlet-name>
    14     <url-pattern>/AccountServlet</url-pattern>
    15   </servlet-mapping>
    16   <welcome-file-list>
    17     <welcome-file>AccountServlet</welcome-file>
    18   </welcome-file-list>
    19 </web-app>
    web.xml

    13.在src下创建applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" 
     3 xmlns:aop="http://www.springframework.org/schema/aop" 
     4 xmlns:context="http://www.springframework.org/schema/context" 
     5 xmlns:tx="http://www.springframework.org/schema/tx" 
     6 xmlns:mvc="http://www.springframework.org/schema/mvc" 
     7 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     8 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     9 xsi:schemaLocation="http://www.springframework.org/schema/beans 
    10 http://www.springframework.org/schema/beans/spring-beans.xsd 
    11 http://www.springframework.org/schema/aop 
    12 http://www.springframework.org/schema/aop/spring-aop.xsd 
    13 http://www.springframework.org/schema/context 
    14 http://www.springframework.org/schema/context/spring-context.xsd 
    15 http://www.springframework.org/schema/tx 
    16 http://www.springframework.org/schema/tx/spring-tx.xsd 
    17 http://www.springframework.org/schema/mvc 
    18 http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    19 ">
    20 <!-- 全局注解参数 -->
    21 <context:annotation-config/>
    22 
    23 <!--全局扫描包  -->
    24 <context:component-scan base-package="com"/>
    25 
    26 
    27 <!-- 1.驱动管理数据源 -->
    28 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    29     <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    30     <property name="url" value="jdbc:mysql://localhost:3306/holly"/>
    31     <property name="username" value="root"/>
    32     <property name="password" value="ok"/>
    33 </bean>
    34 
    35 <!-- 2.sqlsessionfatorybean -->
    36 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    37     <property name="dataSource" ref="dataSource"/>
    38     <property name="mapperLocations">
    39       <value>classpath:com/mapper/*.xml</value>
    40     </property>
    41 </bean>
    42 
    43 <!-- 3.sqlsessionTemplate -->
    44 <!--SqlSessionTemplate sqlSessionTemplate=new SqlSessionTemplate(sqlSessionFactoryBean)-->
    45 <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    46     <constructor-arg ref="sqlSessionFactoryBean"/>
    47 </bean>
    48 
    49 <!-- 4.AccountMapperImpl 
    50 <bean id="accountMapperImpl" class="com.mapper.impl.AccountMapperImpl">
    51     accountMapperImpl.setSqlSessionTemplate(sqlSessionTemplate) 
    52    <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
    53 </bean>
    54 
    55  5.AccountServiceImpl 
    56 <bean id="accountServiceImpl" class="com.service.impl.AccountServiceImpl">
    57     <property name="mapper" ref="accountMapperImpl"/>
    58 </bean>
    59 
    60 --></beans>
    applicationContext.xml

    14.在WebRoot下编辑index.jsp

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 
     8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12     
    13     <title>My JSP 'index.jsp' starting page</title>
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22   </head>
    23   
    24   <body>
    25      <h3><a href="save.jsp">添加</a></h3>
    26       <table>
    27         <tr>
    28           <td>编号</td>
    29           <td>卡号</td>
    30           <td>余额</td>
    31           <td>开户日期</td>
    32           <td>状态</td>
    33           <td>操作</td>
    34         </tr>
    35         <c:forEach var="i" items="${list}">
    36            <tr>
    37              <td>${i.id }</td>
    38              <td>${i.account_number }</td>
    39              <td>${i.account_money }</td>
    40              <td>${i.create_time }</td>
    41              <td>${i.account_status }</td>
    42              <td><a href="AccountServlet?id=${i.id }&flag=findbyid">修改</a>
    43              |
    44              <a href="AccountServlet?id=${i.id }&flag=delete">删除</a></td>
    45            </tr>
    46         </c:forEach>
    47       </table>
    48   </body>
    49 </html>
    index.jsp

    15.运行

  • 相关阅读:
    Jzoj3555 树的直径
    Jzoj3555 树的直径
    51Nod1022 石子归并V2
    51Nod1022 石子归并V2
    Bzoj3998 弦论
    Bzoj3998 弦论
    Poj2758 Checking the Text
    Poj2758 Checking the Text
    常用SQL语句大全
    Silverlight调用GP服务第二篇之调用GP服务(Geoprocessing Service)过程详解
  • 原文地址:https://www.cnblogs.com/holly8/p/7510286.html
Copyright © 2011-2022 走看看