1. 按照如下表结构创建数据库 mydb 及停车场车辆管理表car ( 15 分)数据及表的创建,参考如下:( 10 分)测试数据如下:(5分)
2.完成spring框架和mybatis框架的整合(15分)
使用xml配置或者注解方式完成框架的整合配置
3. 使用 spring 框架整合 mybatis 完成表的增删改查操作( 50 分)1 )完成所有车辆信息的查询;( 15 分)2 )完成车辆信息的新增;( 15 分)3 )完成车辆信息删除;( 10 分)4 )完成车辆信息的修改;( 10 分)4. 使用 AOP 完成功能增强( 20 分)1 )使用 AOP 完成查询功能的后置日志输出;( 10 分)2 )使用 AOP 完成增删改的前置权限验证;( 10 分)
drop table if exists car;
create table car (
cid int(11) primary key not null AUTO_INCREMENT,
cnumber varchar(20),
type varchar(20),
owner varchar(20),
phone varchar(11),
mark varchar(50)
);
insert into car values (1, '豫A34G87', '轿车', '王文琦', '13895647594','临时车');
insert into car values (2, '豫B38379', 'SUV', '张玉梅', '13687594623','月卡');
insert into car values (3, '豫CR9703', '卡车', '刘兆鹏', '13975462146','年卡');
select * from car;
public class Car {
private Integer cid;
private String cnumber;
private String type;
private String owner;
private String phone;
private String mark;
package com.su.dao;
import com.su.bean.Car;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CarDao {
//添加
@Insert("insert into car(cnumber,type,owner,phone,mark) values(#{cnumber},#{type},#{owner},#{phone},#{mark})")
int add(Car car);
//修改
@Update("update car set cnumber=#{cnumber},type=#{type},owner=#{owner},phone=#{phone},mark=#{mark} where cid=#{cid}")
int update(Car car);
//删除
@Delete("delete from car where cid=#{cid}")
int delete(int cid);
//查询
@Select("select * from car")
List selectAll();
}
package com.su.service;
import com.su.bean.Car;
import java.util.List;
public interface CarService {
//查询
List findAll();
//添加
int add(Car car);
//修改
int update(Car car);
//删除
int delete(int cid);
}
package com.su.service.impl;
import com.su.bean.Car;
import com.su.dao.CarDao;
import com.su.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CarServiceImpl implements CarService {
@Autowired
CarDao carDao;
@Override
public List findAll() {
return carDao.selectAll();
}
@Override
public int add(Car car) {
return carDao.add(car);
}
@Override
public int update(Car car) {
return carDao.update(car);
}
@Override
public int delete(int cid) {
return carDao.delete(cid);
}
}
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysql
jdbc.user=root
jdbc.password=root
log4j.properties
log4j.rootLogger=TRACE,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=wocao.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n
log4j.logger.mapperNS =TRACE
log4j.logger.com.mybatis=DEBUG
log4j.logger.com.mybatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.mybatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.org.springframework=error
log4j.logger.org.apache=ERROR
log4j.logger.org.mybatis=DEBUG
package com.su.advice;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
//增强类Advice
@Component
@Aspect
public class Advice {
@Before("execution(int com.su.service.impl.*.*(..))")
public void select() {
System.out.println("前置权限验证.....");
}
@AfterReturning("execution(* com.su.service.impl.*.findAll())")
public void after() {
System.out.println("后置日志输出.....");
}
}
package com.su.servlet;
import com.su.bean.Car;
import com.su.service.CarService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test01 {
CarService carService;
// 全查
@Test
public void findAllTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
carService = context.getBean(CarService.class);
List carList = carService.findAll();
for (Car car : carList) {
System.out.println(car);
}
}
// 添加
@Test
public void addTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
carService = context.getBean(CarService.class);
Car car = new Car();
car.setCnumber("豫P15445");
car.setType("越野车");
car.setOwner("赵四");
car.setPhone("12345678451");
car.setMark("月卡");
int n = carService.add(car);
if (n > 0) {
System.out.println("添加成功!");
}else {
System.out.println("添加失败!");
}
}
// 删除
@Test
public void deleteTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
carService = context.getBean(CarService.class);
int n = carService.delete(2);
if (n > 0) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
}
// 修改
@Test
public void updateTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
carService = context.getBean(CarService.class);
Car car = new Car();
car.setCid(5);
car.setCnumber("豫P15445");
car.setType("越野车");
car.setOwner("张三");
car.setPhone("12345678451");
car.setMark("年卡");
int n = carService.update(car);
if (n > 0) {
System.out.println("修改成功!");
}else{
System.out.println("修改失败!");
}
}
}
全查
添加
修改
删除