使用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. 配置NTP时间服务器

    一.安装ntp软件 1.检查是否安装了ntp相关包. rpm -qa | grep ntp 2.安装ntp软件. yum -y install ntp 二.参数讲解 ignore  :关闭所有的 NT ...

  2. 去掉app右侧滑动时出现的滚动条

    把plus.webview.currentWebview().setStyle({scrollIndicator:'none'});放在mui.plusReady({});内

  3. 网页JavaScript3

    window.document 1,确认元素, document.getElementById("id");           根据id找元素 doucment.getEleme ...

  4. CRC32校验的用法

    CRC32校验数据的完整性 这里的数据包括字符串.文件,还有哪些? 文件校验相当于下载大型软件,有md5加密结果.这里的用途是什么?

  5. jQuery验证框架 .

          目录视图 摘要视图 订阅 “程序人生”中国软件开发者职业生涯调查     CSDN社区“三八节”特别活动      开发者职业生涯调查之未来 jQuery验证框架 分类: JQuery 2 ...

  6. Oracle 增加修改删除字段

    Oracle 增加修改删除字段 添加字段的语法:alter table tablename add (column datatype [default value][null/not null],…. ...

  7. 关于华为交换机bpdu enable. ntdp enable. ndp enable解析

    华为5300初始状态下每个口子都有,bpdu enable. ntdp enable. ndp enable.不是很明白什么意思,有什么样的用途. BPDU是网桥协议数据单元(Bridge Proto ...

  8. 静默安装ORACLE【weber出品必属精品】

    安装配置系统环境安装linux ,所有服务都不选择,只是选择安装开发工具,不要安装防火墙(当然也可以在后面关闭) 打开终端,执行如下命令,检查安装包,没有的都要安装 make, glibc, liba ...

  9. Php RSS

    RSS 聚合最近非常流行,因此至少对 RSS 及其工作方式有所了解是一名 PHP 开发人员的迫切需要.本文介绍了 RSS 基础知识.RSS 众多用途中的一些用途.如何使用 PHP 从数据库创建 RSS ...

  10. HTML5 初始文档声明

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...