使用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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>SpringMybatis</groupId> 4 <artifactId>SpringMybatis</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 <name>SpringMybatis</name> 8 <description/> 9 <properties> 10 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 11 <!-- spring版本号 --> 12 <spring.version>4.0.2.RELEASE</spring.version> 13 <!-- mybatis版本号 --> 14 <mybatis.version>3.2.6</mybatis.version> 15 16 <!-- log4j日志文件管理包版本 --> 17 <slf4j.version>1.7.7</slf4j.version> 18 <log4j.version>1.2.17</log4j.version> 19 </properties> 20 <dependencies> 21 <dependency> 22 <groupId>javax</groupId> 23 <artifactId>javaee-api</artifactId> 24 <version>7.0</version> 25 <scope>provided</scope> 26 </dependency> 27 <dependency> 28 <groupId>org.glassfish.web</groupId> 29 <artifactId>javax.servlet.jsp.jstl</artifactId> 30 <version>1.2.2</version> 31 </dependency> 32 33 34 35 <dependency> 36 <groupId>org.aspectj</groupId> 37 <artifactId>aspectjrt</artifactId> 38 <version>1.6.11</version> 39 </dependency> 40 41 42 <dependency> 43 <groupId>org.aspectj</groupId> 44 <artifactId>aspectjweaver</artifactId> 45 <version>1.6.11</version> 46 </dependency> 47 48 <!-- spring核心包 --> 49 50 51 52 <dependency> 53 54 <groupId>org.springframework</groupId> 55 56 <artifactId>spring-core</artifactId> 57 58 <version>${spring.version}</version> 59 60 </dependency> 61 62 63 <dependency> 64 65 <groupId>org.springframework</groupId> 66 67 <artifactId>spring-web</artifactId> 68 69 <version>${spring.version}</version> 70 71 </dependency> 72 73 74 <dependency> 75 76 <groupId>org.springframework</groupId> 77 78 <artifactId>spring-oxm</artifactId> 79 80 <version>${spring.version}</version> 81 82 </dependency> 83 84 85 <dependency> 86 87 <groupId>org.springframework</groupId> 88 89 <artifactId>spring-tx</artifactId> 90 91 <version>${spring.version}</version> 92 93 </dependency> 94 95 96 <dependency> 97 98 <groupId>org.springframework</groupId> 99 100 <artifactId>spring-jdbc</artifactId> 101 102 <version>${spring.version}</version> 103 104 </dependency> 105 106 107 <dependency> 108 109 <groupId>org.springframework</groupId> 110 111 <artifactId>spring-webmvc</artifactId> 112 113 <version>${spring.version}</version> 114 115 </dependency> 116 117 118 <dependency> 119 120 <groupId>org.springframework</groupId> 121 122 <artifactId>spring-aop</artifactId> 123 124 <version>${spring.version}</version> 125 126 </dependency> 127 128 129 <dependency> 130 131 <groupId>org.springframework</groupId> 132 133 <artifactId>spring-context-support</artifactId> 134 135 <version>${spring.version}</version> 136 137 </dependency> 138 139 140 <dependency> 141 142 <groupId>org.springframework</groupId> 143 144 <artifactId>spring-test</artifactId> 145 146 <version>${spring.version}</version> 147 148 </dependency> 149 150 151 <!-- mybatis核心包 --> 152 153 <dependency> 154 155 <groupId>org.mybatis</groupId> 156 157 <artifactId>mybatis</artifactId> 158 159 <version>${mybatis.version}</version> 160 161 </dependency> 162 163 164 <dependency> 165 <groupId>commons-dbcp</groupId> 166 <artifactId>commons-dbcp</artifactId> 167 <version>1.4</version> 168 </dependency> 169 170 <dependency> 171 <groupId>commons-pool</groupId> 172 <artifactId>commons-pool</artifactId> 173 <version>1.6</version> 174 </dependency> 175 176 177 <!-- https://mvnrepository.com/artifact/log4j/log4j --> 178 <dependency> 179 <groupId>log4j</groupId> 180 <artifactId>log4j</artifactId> 181 <version>1.2.17</version> 182 </dependency> 183 184 185 <!-- mybatis/spring包 --> 186 187 188 189 <dependency> 190 191 <groupId>org.mybatis</groupId> 192 193 <artifactId>mybatis-spring</artifactId> 194 195 <version>1.2.2</version> 196 197 </dependency> 198 199 200 201 <!-- 导入Mysql数据库链接jar包 --> 202 203 204 205 <dependency> 206 <groupId>mysql</groupId> 207 <artifactId>mysql-connector-java</artifactId> 208 <version>5.1.30</version> 209 </dependency> 210 211 </dependencies> 212 <build> 213 <plugins> 214 <plugin> 215 <artifactId>maven-compiler-plugin</artifactId> 216 <version>2.3.2</version> 217 <configuration> 218 <source>1.7</source> 219 <target>1.7</target> 220 </configuration> 221 </plugin> 222 <plugin> 223 <artifactId>maven-war-plugin</artifactId> 224 <version>2.2</version> 225 <configuration> 226 <version>3.1</version> 227 <failOnMissingWebXml>false</failOnMissingWebXml> 228 </configuration> 229 </plugin> 230 </plugins> 231 </build> 232 </project>
Spring-Mybatis配置文件 ApplicationContext.xml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 13 http://www.springframework.org/schema/context 14 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 15 <!-- 自动扫描 --> 16 <context:component-scan base-package="com.hand" /> 17 <!-- aop配置!!! --> 18 <aop:aspectj-autoproxy /> 19 20 <!-- 引入配置文件 --> 21 <bean id="propertyConfigurer" 22 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 23 <property name="location" value="classpath:jdbc.properties" /> 24 </bean> 25 26 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 27 destroy-method="close"> 28 <property name="driverClassName" value="${driver}" /> 29 <property name="url" value="${url}" /> 30 <property name="username" value="${username}" /> 31 <property name="password" value="${password}" /> 32 <!-- 初始化连接大小 --> 33 <property name="initialSize" value="${initialSize}"></property> 34 <!-- 连接池最大数量 --> 35 <property name="maxActive" value="${maxActive}"></property> 36 <!-- 连接池最大空闲 --> 37 <property name="maxIdle" value="${maxIdle}"></property> 38 <!-- 连接池最小空闲 --> 39 <property name="minIdle" value="${minIdle}"></property> 40 <!-- 获取连接最大等待时间 --> 41 <property name="maxWait" value="${maxWait}"></property> 42 </bean> 43 44 <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> 45 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 46 <property name="dataSource" ref="dataSource" /> 47 <!-- 自动扫描mapping.xml文件 --> 48 <property name="mapperLocations" value="classpath:com/hand/mapping/*.xml"></property> 49 </bean> 50 51 <!-- DAO接口所在包名,Spring会自动查找其下的类 --> 52 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 53 <property name="basePackage" value="com.hand.dao.impl" /> 54 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 55 </bean> 56 57 <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> 58 <bean id="transactionManager" 59 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 60 <property name="dataSource" ref="dataSource" /> 61 </bean> 62 63 <!-- 配置SqlSessionTemplate --> 64 <bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> 65 <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" /> 66 </bean> 67 </beans>
数据库配置文件 jdbc.properties
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 driver=com.mysql.jdbc.Driver 2 url=jdbc\:mysql\://localhost\:3306/sakila 3 username=root 4 password= 5 #\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570 6 initialSize=0 7 #\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570 8 maxActive=20 9 #\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2 10 maxIdle=20 11 #\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2 12 minIdle=1 13 #\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4 14 maxWait=60000
log4j配置文件 log4j.properties
1 log4j.rootLogger=DEBUG, Console 2 3 #Console 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender 5 log4j.appender.Console.layout=org.apache.log4j.PatternLayout 6 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n 7 8 log4j.logger.java.sql.ResultSet=INFO 9 log4j.logger.org.apache=INFO 10 log4j.logger.java.sql.Connection=DEBUG 11 log4j.logger.java.sql.Statement=DEBUG 12 log4j.logger.java.sql.PreparedStatement=DEBUG
com.hand.aop包下的AOP类 AopClass.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.hand.aop; 2 3 import org.aspectj.lang.annotation.After; 4 import org.aspectj.lang.annotation.Aspect; 5 import org.aspectj.lang.annotation.Before; 6 import org.springframework.stereotype.Component; 7 8 9 @Aspect 10 @Component 11 public class AopClass { 12 13 @Before("execution(* com.hand.dao.impl.CustomerDaoImpl.insertToCustomer(..))") 14 public void before(){ 15 System.out.println("Before Insert Customer Data"); 16 } 17 @After("execution(* com.hand.dao.impl.CustomerDaoImpl.insertToCustomer(..))") 18 public void after(){ 19 System.out.println("After Insert Customer Data"); 20 } 21 }
CustomerDaoImpl.java(数据库查询的具体实现逻辑)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.hand.dao.impl; 2 3 import javax.annotation.Resource; 4 5 import org.apache.ibatis.session.SqlSession; 6 import org.apache.ibatis.session.SqlSessionFactory; 7 import org.mybatis.spring.SqlSessionTemplate; 8 import org.springframework.stereotype.Repository; 9 10 import com.hand.model.CustomerInfo; 11 12 13 @Repository 14 public class CustomerDaoImpl{ 15 16 SqlSessionTemplate sessionTemplate; 17 18 public SqlSessionTemplate getSessionTemplate() { 19 return sessionTemplate; 20 } 21 22 @Resource(name="sessionTemplate") 23 public void setSessionTemplate(SqlSessionTemplate sessionTemplate) { 24 this.sessionTemplate = sessionTemplate; 25 } 26 27 28 29 public int selectAddressId(int address_id) { 30 int co = sessionTemplate.selectOne("com.hand.model.CustomerInfo.selectAddressId",address_id); 31 return co; 32 } 33 34 public void insertToCustomer(CustomerInfo customer) { 35 36 SqlSessionFactory factory = sessionTemplate.getSqlSessionFactory(); 37 SqlSession session = factory.openSession(); 38 sessionTemplate.insert("com.hand.model.CustomerInfo.insertToCustomer", customer); 39 int id = session.selectOne("com.hand.model.CustomerInfo.findUpdate"); 40 session.close(); 41 42 43 CustomerInfo cus = sessionTemplate.selectOne("com.hand.model.CustomerInfo.findCustomer", id); 44 System.out.println("id:"+cus.getCustomer_id()); 45 System.out.println("First_name:"+cus.getFirst_name()); 46 System.out.println("Last_name:"+cus.getLast_name()); 47 System.out.println("Email:"+cus.getEmail()); 48 System.out.println("Address:"+cus.getAddress_id()); 49 50 } 51 52 public void deleteCustomer(int delete_id) { 53 sessionTemplate.delete("com.hand.model.CustomerInfo.deleteCustomer",delete_id); 54 } 55 56 }
ICustomerDao.xml(数据库的查询语句控制文件)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 5 6 <mapper namespace="com.hand.model.CustomerInfo"> 7 8 <select id="selectAddressId" parameterType="int" resultType="int"> 9 select count(*) from address where address_id = #{address_id}; 10 </select> 11 12 <insert id="insertToCustomer" parameterType="com.hand.model.CustomerInfo"> 13 insert into customer (first_name,last_name,email,address_id,create_date,store_id) 14 values(#{first_name},#{last_name},#{email},#{address_id},#{create_date},#{store_id}); 15 </insert> 16 17 <select id="findUpdate" resultType="int"> 18 select DISTINCT LAST_INSERT_id() from customer; 19 </select> 20 21 <select id="findCustomer" parameterType="int" resultType="com.hand.model.CustomerInfo"> 22 select * from customer where customer_id = #{id}; 23 </select> 24 25 <delete id="deleteCustomer" parameterType="int"> 26 delete from customer where customer_id = #{delete_id}; 27 </delete> 28 29 </mapper>
customer表的实体类 CustomerInfo.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.hand.model; 2 3 public class CustomerInfo { 4 5 private int customer_id; 6 private int store_id; 7 private String first_name; 8 private String last_name; 9 private String email; 10 private int address_id; 11 private int active; 12 private String create_date; 13 private String last_update; 14 public int getCustomer_id() { 15 return customer_id; 16 } 17 public void setCustomer_id(int customer_id) { 18 this.customer_id = customer_id; 19 } 20 public int getStore_id() { 21 return store_id; 22 } 23 public void setStore_id(int store_id) { 24 this.store_id = store_id; 25 } 26 public String getFirst_name() { 27 return first_name; 28 } 29 public void setFirst_name(String first_name) { 30 this.first_name = first_name; 31 } 32 public String getLast_name() { 33 return last_name; 34 } 35 public void setLast_name(String last_name) { 36 this.last_name = last_name; 37 } 38 public String getEmail() { 39 return email; 40 } 41 public void setEmail(String email) { 42 this.email = email; 43 } 44 public int getAddress_id() { 45 return address_id; 46 } 47 public void setAddress_id(int address_id) { 48 this.address_id = address_id; 49 } 50 public int getActive() { 51 return active; 52 } 53 public void setActive(int active) { 54 this.active = active; 55 } 56 public String getCreate_date() { 57 return create_date; 58 } 59 public void setCreate_date(String create_date) { 60 this.create_date = create_date; 61 } 62 public String getLast_update() { 63 return last_update; 64 } 65 public void setLast_update(String last_update) { 66 this.last_update = last_update; 67 } 68 69 }
ICustomerService.java(service类接口)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.hand.service; 2 3 import com.hand.model.CustomerInfo; 4 5 6 7 public interface ICustomerService { 8 9 public int selectAddressId(int address_id); 10 11 public void insertToCustomer(CustomerInfo customer); 12 13 public void delete_customerId(int delete_id); 14 }
CustomerServiceImpl.java(service实现类)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.hand.service.impl; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.stereotype.Service; 6 7 import com.hand.dao.impl.CustomerDaoImpl; 8 import com.hand.model.CustomerInfo; 9 import com.hand.service.ICustomerService; 10 11 12 @Service("customerService") 13 public class CustomerServiceImpl implements ICustomerService{ 14 15 @Resource 16 private CustomerDaoImpl iCustomerDao; 17 18 @Override 19 public int selectAddressId(int address_id) { 20 21 return this.iCustomerDao.selectAddressId(address_id); 22 } 23 24 @Override 25 public void insertToCustomer(CustomerInfo customer) { 26 this.iCustomerDao.insertToCustomer(customer); 27 } 28 29 @Override 30 public void delete_customerId(int delete_id) { 31 this.iCustomerDao.deleteCustomer(delete_id); 32 } 33 34 35 36 }
测试类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.hand.test; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 import java.util.Scanner; 6 7 import javax.annotation.Resource; 8 9 import org.springframework.context.ApplicationContext; 10 import org.springframework.context.support.ClassPathXmlApplicationContext; 11 12 import com.hand.model.CustomerInfo; 13 import com.hand.service.ICustomerService; 14 import com.hand.service.impl.CustomerServiceImpl; 15 16 17 18 public class Main { 19 20 21 public static void main(String[] args) { 22 23 24 25 26 ApplicationContext act = new ClassPathXmlApplicationContext("ApplicationContext.xml"); 27 CustomerServiceImpl customerService = (CustomerServiceImpl) act.getBean("customerService"); 28 29 30 31 CustomerInfo customer = new CustomerInfo(); 32 Scanner scanner = new Scanner(System.in); 33 34 String username; 35 String lastname; 36 String email; 37 int address_id; 38 String create_date; 39 int delete_id; 40 41 System.out.println("请输入firstname"); 42 username = scanner.next(); 43 44 System.out.println("请输入lastname"); 45 lastname = scanner.next(); 46 47 System.out.println("请输入email"); 48 email = scanner.next(); 49 50 System.out.println("请输入address_id"); 51 address_id = scanner.nextInt(); 52 53 int flag = 0; 54 flag = customerService.selectAddressId(address_id); 55 //System.out.println("flag="+flag); 56 while(true){ 57 if(flag>0){ 58 break; 59 }else{ 60 System.out.println("address_id不存在,请重新输入 "); 61 System.out.println("请输入address_id"); 62 address_id = scanner.nextInt(); 63 flag = customerService.selectAddressId(address_id); 64 } 65 } 66 67 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式 68 create_date = df.format(new Date()); 69 customer.setStore_id(1); 70 customer.setFirst_name(username); 71 customer.setLast_name(lastname); 72 customer.setEmail(email); 73 customer.setAddress_id(address_id); 74 customer.setCreate_date(create_date); 75 customerService.insertToCustomer(customer); 76 77 78 System.out.println("请输入要删除的Customer的ID"); 79 delete_id = scanner.nextInt(); 80 customerService.delete_customerId(delete_id); 81 System.out.println("你输入的ID已经删除"); 82 83 } 84 85 }
运行结果