使用Spring与Mybatis技术实现下要求:

(2分)1,Spring的配置文件名称为ApplicationContext.xml

(2分)2,在ApplicationContext.xml中配置Mybatis.

(6分)3,为数据库中的country,city,address,customer,store表添加 dto,mapper,service接口及service.

(6分)4,从命令行中接收Customer信息,并将数据保存至数据库: store_id:自动设置为1 firsrt_name: 从命令行中输入 last_name: 从命令行中输入 email: 从命令行中输入 address_id: 从命令行中输入,如果发现ID不存在则提示用户重新输 入. create_date: 取得当前时间

(2分)5,从数据中查询出4中保存的数据,并按规定的格式输出. 显示的字段有first_name,last_name,email,address,create_date

(6分)6,使用AOP实现,在将film数据插入数据库表中之前publish Spring的事件BeforeInsertFilmEvent,在将film插入数据库之后 publish Spring的事件 AfterInsertFilmEvent.       (2分)7,接收到BeforeInsertFilmEvent事件,输出Before Insert Customer Data.

(2分)8,接收到AfterInsertFilmEvent事件,输出After Insert Customer Data.
    (4分)9,上面所有步骤完成后等待用户输入一个customer id,如果ID 存在则删除对应的数据,如果不存在则提示用户再次输入.

(6分)10,所有的数据库操作都添加事务管理,事务管理需要使用 spring配置.

示例:

请输入FirstName(first_name): xxxxx

请输入LastName(last_name): xxxxxxxxxxxx

请输入Email(email): xxx@xx.com

请输入Address ID: 1000

你输入的Address ID不存在,请重新输入: 605

Before Insert Customer Data

已经保存的数据如下:

ID: xxxxx

FirstName: xxxxx

LastName: xxxx

Email:xxxxx Address: 1325 Fukuyama Street

After Insert Customer Data

请输入要删除的Customer的ID: xxxxx

你输入的ID为xxx的Customer已经 删除.

项目结构

Maven项目代码:

Maven配置文件 pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion></modelVersion>
   <groupId>SpringMybatis</groupId>
   <artifactId>SpringMybatis</artifactId>
   <version>-SNAPSHOT</version>
   <packaging>war</packaging>
   <name>SpringMybatis</name>
   <description/>
   <properties>
     <project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
     <!-- spring版本号 -->
       <spring.version>.RELEASE</spring.version>
     <!-- mybatis版本号 -->
       <mybatis.version></mybatis.version>

     <!-- log4j日志文件管理包版本 -->
       <slf4j.version></slf4j.version>
       <log4j.version></log4j.version>
   </properties>
   <dependencies>
     <dependency>
       <groupId>javax</groupId>
       <artifactId>javaee-api</artifactId>
       <version>7.0</version>
       <scope>provided</scope>
     </dependency>
     <dependency>
       <groupId>org.glassfish.web</groupId>
       <artifactId>javax.servlet.jsp.jstl</artifactId>
       <version></version>
     </dependency>

     <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjrt</artifactId>
         <version></version>
     </dependency>

     <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjweaver</artifactId>
         <version></version>
     </dependency>

     <!-- spring核心包 -->

     <dependency>

     <groupId>org.springframework</groupId>

     <artifactId>spring-core</artifactId>

     <version>${spring.version}</version>

     </dependency>

     <dependency>

     <groupId>org.springframework</groupId>

     <artifactId>spring-web</artifactId>

     <version>${spring.version}</version>

     </dependency>

     <dependency>

     <groupId>org.springframework</groupId>

     <artifactId>spring-oxm</artifactId>

     <version>${spring.version}</version>

     </dependency>

     <dependency>

     <groupId>org.springframework</groupId>

     <artifactId>spring-tx</artifactId>

     <version>${spring.version}</version>

     </dependency>

     <dependency>

     <groupId>org.springframework</groupId>

     <artifactId>spring-jdbc</artifactId>

     <version>${spring.version}</version>

     </dependency>

     <dependency>

     <groupId>org.springframework</groupId>

     <artifactId>spring-webmvc</artifactId>

     <version>${spring.version}</version>

     </dependency>

     <dependency>

     <groupId>org.springframework</groupId>

     <artifactId>spring-aop</artifactId>

     <version>${spring.version}</version>

     </dependency>

     <dependency>

     <groupId>org.springframework</groupId>

     <artifactId>spring-context-support</artifactId>

     <version>${spring.version}</version>

     </dependency>

     <dependency>

     <groupId>org.springframework</groupId>

     <artifactId>spring-test</artifactId>

     <version>${spring.version}</version>

     </dependency>

     <!-- mybatis核心包 -->

     <dependency>

     <groupId>org.mybatis</groupId>

     <artifactId>mybatis</artifactId>

     <version>${mybatis.version}</version>

     </dependency>

     <dependency>
         <groupId>commons-dbcp</groupId>
         <artifactId>commons-dbcp</artifactId>
         <version>1.4</version>
     </dependency>

     <dependency>
         <groupId>commons-pool</groupId>
         <artifactId>commons-pool</artifactId>
         <version>1.6</version>
     </dependency>

     <!-- https://mvnrepository.com/artifact/log4j/log4j -->
     <dependency>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
         <version></version>
     </dependency>

     <!-- mybatis/spring包 -->

     <dependency>

     <groupId>org.mybatis</groupId>

     <artifactId>mybatis-spring</artifactId>

     <version></version>

     </dependency>

     <!-- 导入Mysql数据库链接jar包 -->

     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version></version>
     </dependency>

   </dependencies>
   <build>
     <plugins>
       <plugin>
         <artifactId>maven-compiler-plugin</artifactId>
         <version></version>
         <configuration>
           <source>1.7</source>
           <target>1.7</target>
         </configuration>
       </plugin>
       <plugin>
         <artifactId>maven-war-plugin</artifactId>
         <version>2.2</version>
         <configuration>
           <version>3.1</version>
           <failOnMissingWebXml>false</failOnMissingWebXml>
         </configuration>
       </plugin>
     </plugins>
   </build>
 </project>

Spring-Mybatis配置文件  ApplicationContext.xml

 <?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:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
     <!-- 自动扫描 -->
     <context:component-scan base-package="com.hand" />
     <!-- aop配置!!! -->
     <aop:aspectj-autoproxy /> 

     <!-- 引入配置文件 -->
     <bean id="propertyConfigurer"
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="location" value="classpath:jdbc.properties" />
     </bean>  

     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
         destroy-method="close">
         <property name="driverClassName" value="${driver}" />
         <property name="url" value="${url}" />
         <property name="username" value="${username}" />
         <property name="password" value="${password}" />
         <!-- 初始化连接大小 -->
         <property name="initialSize" value="${initialSize}"></property>
         <!-- 连接池最大数量 -->
         <property name="maxActive" value="${maxActive}"></property>
         <!-- 连接池最大空闲 -->
         <property name="maxIdle" value="${maxIdle}"></property>
         <!-- 连接池最小空闲 -->
         <property name="minIdle" value="${minIdle}"></property>
         <!-- 获取连接最大等待时间 -->
         <property name="maxWait" value="${maxWait}"></property>
     </bean>  

     <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <property name="dataSource" ref="dataSource" />
         <!-- 自动扫描mapping.xml文件 -->
         <property name="mapperLocations" value="classpath:com/hand/mapping/*.xml"></property>
     </bean>  

     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="com.hand.dao.impl" />
         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
     </bean>  

     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
     <bean id="transactionManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource" />
     </bean>  

      <!-- 配置SqlSessionTemplate -->
     <bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
         <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
     </bean>
 </beans>  

数据库配置文件  jdbc.properties

 driver=com.mysql.jdbc.Driver
 url=jdbc\:mysql\://localhost\:3306/sakila
 username=root
 password=
 #\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
 initialSize=
 #\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
 maxActive=
 #\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
 maxIdle=
 #\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
 minIdle=
 #\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
 maxWait=  

log4j配置文件 log4j.properties

 log4j.rootLogger=DEBUG, Console  

 #Console
 log4j.appender.Console=org.apache.log4j.ConsoleAppender
 log4j.appender.Console.layout=org.apache.log4j.PatternLayout
 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  

 log4j.logger.java.sql.ResultSet=INFO
 log4j.logger.org.apache=INFO
 log4j.logger.java.sql.Connection=DEBUG
 log4j.logger.java.sql.Statement=DEBUG
 log4j.logger.java.sql.PreparedStatement=DEBUG

com.hand.aop包下的AOP类 AopClass.java

 package com.hand.aop;

 import org.aspectj.lang.annotation.After;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
 import org.springframework.stereotype.Component;

 @Aspect
 @Component
 public class AopClass {

     @Before("execution(* com.hand.dao.impl.CustomerDaoImpl.insertToCustomer(..))")
     public void before(){
         System.out.println("Before Insert Customer Data");
     }
     @After("execution(* com.hand.dao.impl.CustomerDaoImpl.insertToCustomer(..))")
     public void after(){
         System.out.println("After Insert Customer Data");
     }
 }

CustomerDaoImpl.java(数据库查询的具体实现逻辑)

 package com.hand.dao.impl;

 import javax.annotation.Resource;

 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.mybatis.spring.SqlSessionTemplate;
 import org.springframework.stereotype.Repository;

 import com.hand.model.CustomerInfo;

 @Repository
 public class CustomerDaoImpl{

     SqlSessionTemplate sessionTemplate;

     public SqlSessionTemplate getSessionTemplate() {
         return sessionTemplate;
     }

     @Resource(name="sessionTemplate")
     public void setSessionTemplate(SqlSessionTemplate sessionTemplate) {
         this.sessionTemplate = sessionTemplate;
     }

     public int selectAddressId(int address_id) {
         int co = sessionTemplate.selectOne("com.hand.model.CustomerInfo.selectAddressId",address_id);
         return co;
     }

     public void insertToCustomer(CustomerInfo customer) {

         SqlSessionFactory factory = sessionTemplate.getSqlSessionFactory();
         SqlSession session = factory.openSession();
         sessionTemplate.insert("com.hand.model.CustomerInfo.insertToCustomer", customer);
         int id = session.selectOne("com.hand.model.CustomerInfo.findUpdate");
         session.close();

         CustomerInfo cus = sessionTemplate.selectOne("com.hand.model.CustomerInfo.findCustomer", id);
         System.out.println("id:"+cus.getCustomer_id());
         System.out.println("First_name:"+cus.getFirst_name());
         System.out.println("Last_name:"+cus.getLast_name());
         System.out.println("Email:"+cus.getEmail());
         System.out.println("Address:"+cus.getAddress_id());

     }

     public void deleteCustomer(int delete_id) {
         sessionTemplate.delete("com.hand.model.CustomerInfo.deleteCustomer",delete_id);
     }

 }

ICustomerDao.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.hand.model.CustomerInfo">  

     <select id="selectAddressId" parameterType="int" resultType="int">
          select count(*) from address where address_id = #{address_id};
     </select>  

     <insert id="insertToCustomer" parameterType="com.hand.model.CustomerInfo">
          insert into customer (first_name,last_name,email,address_id,create_date,store_id)
          values(#{first_name},#{last_name},#{email},#{address_id},#{create_date},#{store_id});
     </insert>

     <select id="findUpdate" resultType="int">
        select DISTINCT LAST_INSERT_id() from customer;
     </select>

     <select id="findCustomer" parameterType="int" resultType="com.hand.model.CustomerInfo">
        select * from customer where customer_id = #{id};
     </select>

     <delete id="deleteCustomer" parameterType="int">
        delete from customer where customer_id = #{delete_id};
     </delete>

 </mapper>

customer表的实体类 CustomerInfo.java

 package com.hand.model;

 public class CustomerInfo {

     private int customer_id;
     private int store_id;
     private String first_name;
     private String last_name;
     private String email;
     private int address_id;
     private int active;
     private String create_date;
     private String last_update;
     public int getCustomer_id() {
         return customer_id;
     }
     public void setCustomer_id(int customer_id) {
         this.customer_id = customer_id;
     }
     public int getStore_id() {
         return store_id;
     }
     public void setStore_id(int store_id) {
         this.store_id = store_id;
     }
     public String getFirst_name() {
         return first_name;
     }
     public void setFirst_name(String first_name) {
         this.first_name = first_name;
     }
     public String getLast_name() {
         return last_name;
     }
     public void setLast_name(String last_name) {
         this.last_name = last_name;
     }
     public String getEmail() {
         return email;
     }
     public void setEmail(String email) {
         this.email = email;
     }
     public int getAddress_id() {
         return address_id;
     }
     public void setAddress_id(int address_id) {
         this.address_id = address_id;
     }
     public int getActive() {
         return active;
     }
     public void setActive(int active) {
         this.active = active;
     }
     public String getCreate_date() {
         return create_date;
     }
     public void setCreate_date(String create_date) {
         this.create_date = create_date;
     }
     public String getLast_update() {
         return last_update;
     }
     public void setLast_update(String last_update) {
         this.last_update = last_update;
     }

 }

ICustomerService.java(service类接口)

 package com.hand.service;

 import com.hand.model.CustomerInfo;

 public interface ICustomerService {

     public int selectAddressId(int address_id);

     public void insertToCustomer(CustomerInfo customer);

     public void delete_customerId(int delete_id);
 }

CustomerServiceImpl.java(service实现类)

 package com.hand.service.impl;

 import javax.annotation.Resource;

 import org.springframework.stereotype.Service;

 import com.hand.dao.impl.CustomerDaoImpl;
 import com.hand.model.CustomerInfo;
 import com.hand.service.ICustomerService;

 @Service("customerService")
 public class CustomerServiceImpl implements ICustomerService{

     @Resource
     private CustomerDaoImpl iCustomerDao;

     @Override
     public int selectAddressId(int address_id) {

         return this.iCustomerDao.selectAddressId(address_id);
     }

     @Override
     public void insertToCustomer(CustomerInfo customer) {
         this.iCustomerDao.insertToCustomer(customer);
     }

     @Override
     public void delete_customerId(int delete_id) {
         this.iCustomerDao.deleteCustomer(delete_id);
     }

 }

测试类

 package com.hand.test;

 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Scanner;

 import javax.annotation.Resource;

 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;

 import com.hand.model.CustomerInfo;
 import com.hand.service.ICustomerService;
 import com.hand.service.impl.CustomerServiceImpl;

 public class Main {

     public static void main(String[] args) {

         ApplicationContext act = new ClassPathXmlApplicationContext("ApplicationContext.xml");
         CustomerServiceImpl customerService = (CustomerServiceImpl) act.getBean("customerService");

         CustomerInfo customer = new CustomerInfo();
         Scanner scanner = new Scanner(System.in);

         String username;
         String lastname;
         String email;
         int address_id;
         String create_date;
         int delete_id;

         System.out.println("请输入firstname");
         username = scanner.next();

         System.out.println("请输入lastname");
         lastname = scanner.next();

         System.out.println("请输入email");
         email = scanner.next();

         System.out.println("请输入address_id");
         address_id = scanner.nextInt();

         ;
         flag = customerService.selectAddressId(address_id);
         //System.out.println("flag="+flag);
         while(true){
             ){
                 break;
             }else{
                 System.out.println("address_id不存在,请重新输入 ");
                 System.out.println("请输入address_id");
                 address_id = scanner.nextInt();
                 flag = customerService.selectAddressId(address_id);
             }
         }

         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
         create_date = df.format(new Date());
         customer.setStore_id();
         customer.setFirst_name(username);
         customer.setLast_name(lastname);
         customer.setEmail(email);
         customer.setAddress_id(address_id);

customer.setCreate_date(create_date);
         customerService.insertToCustomer(customer);

         System.out.println("请输入要删除的Customer的ID");
         delete_id = scanner.nextInt();
         customerService.delete_customerId(delete_id);
         System.out.println("你输入的ID已经删除");

     }

 }

运行结果

hand第四次考核的更多相关文章

  1. 我所经历的SAP选型

    这是一个失败的选型项目,而且在可遇见的未来公司也不会再经历SAP选型,甚至不会再启动erp项目,个中原因很难一言道尽,在此简要的说说我们的选型过程以及在选型过程中对各种因素的考虑. 一.重启选型工作七 ...

  2. 0课程介绍(Week1,3月3日)

    一.自我介绍 1.姓名:杨晔 2.办公室:B211-2 3.电子邮件:yangye@zjjy.com.cn 4.QQ:6706892 5.博客:http://www.cnblogs.com/meety ...

  3. 安卓自定义类似TabHost的导航栏

    有时候为了项目需要我们要自定义一些导航控件,类似下面这样. 下面给大家讲讲我是怎么实现的, 1.素材准备(这个都是美工的事情) 2.①资源文件共有五个 如下: activity_main_first. ...

  4. 我所经历的SAP选型[转]

    这是一个失败的选型项目,而且在可遇见的未来公司也不会再经历SAP选型,甚至不会再启动erp项目,个中原因很难一言道尽,在此简要的说说我们的选型过程以及在选型过程中对各种因素的考虑. 一.重启选型工作七 ...

  5. takeLatest 如何接受 this.props.dispatch 传递的参数

    1.步骤一 // 获取查询参数 getQueryParams(params){ // 请求月考核分的数据 this.props.dispatch({ type:'getMonthlyAssessmen ...

  6. Java作业:第四次过程性考核 ——长春职业技术学院 16级网络工程

    Java作业:第四次过程性考核 码云链接:https://gitee.com/SoridoD/java_kaohe4    (时间匆忙没打注释,真有急事) (客户端和服务器会自动创建表,所以没有sql ...

  7. 第四模块:网络编程进阶&数据库开发 考核实战

     1.什么是进程?什么是线程? 什么是协程? 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 线程:在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 协程是一种用 ...

  8. css考核点整理(四)-css盒模型

    http://paranimage.com/css-box-model/

  9. python第四十三天--第三模块考核

    面向对象: 概念:类,实例化,对象,实例 属性: 公有属性:在类中定义 成员属性:在方法中定义 私有属性:在方法中使用 __属性  定义 限制外部访问 方法: 普通方法 类方法: @classmeth ...

随机推荐

  1. Lombok介绍及使用方法

    lombok简介 lombok是暑假来到公司实习的时候发现的一个非常好用的小工具,刚见到的时候就感觉非常惊艳,有一种相见恨晚的感觉,用了一段时间之后感觉的确挺不错,所以特此来推荐一下. lombok的 ...

  2. HDu -2844 Coins多重背包

    这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就 ...

  3. 内存泄露 Memory Leaks

    什么是内存泄露 内存管理一直是Java 所鼓吹的强大优点.开发者只需要简单地创建对象,而Java的垃圾收集器将会自动管理内存空间的分配和释放. 但在很多情况下,事情并不那么简单,在 Java程序中总是 ...

  4. IIS 7.5 配置伪静态

    IIS 7.5 配置伪静态_win服务器_脚本之家 win7下IIS的安装和配置 图文教程详细出处参考:http://www.jb51.net/article/29787.htm http://blo ...

  5. ReportViewer2010冻结行列

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NewTrackingVer ...

  6. PHP Socket编程起步

    让我们以一个简单的例子开始---一个接收输入字符串,处理并返回这个字符串到客户端的TCP服务.下面是相应的代码: PHP 代码: ) or die("Could not read input ...

  7. Android开发环境搭建简介

    Android的开发工具,可以使用Eclipse,Idea,Android Studio,其中Eclipse是开源中国大部分使用的IDE,Idea是号称最好用的开发工具,有很多用处,Android S ...

  8. PartialViewResult用法

    后台代码 ) { IList<TestModel> lstTestModel = this.GetModelList(categoryid); return PartialView(lst ...

  9. Spring Boot Web项目之参数绑定

    一.@RequestParam 这个注解用来绑定单个请求数据,既可以是url中的参数,也可以是表单提交的参数和上传的文件 它有三个属性,value用于设置参数名,defaultValue用于对参数设置 ...

  10. Android导入项目时出现红色感叹号

    导入一个新的项目后,丢失android.jar文件 解决方法:在项目名称上单击右键,选择Properties,再选择Android,再在其中选择一个project build target,点击确定之 ...