一、lombok插件
1. 功能:对实体类自动,动态生成get、set方法,无参、有参构造.....
2. 步骤:
(1)idea安装插件(只做一次)
(2)添加坐标
(3)编写注解
@NoArgsConstructor :无参构造
@AllArgsConstructor :全参构造
@Data :get、set、toString方法
package com.apesource.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@NoArgsConstructor // 无参
@AllArgsConstructor // 全参
@Data // get、set、toString方法
public class Account implements Serializable {
private int aid;
private String aname;
private int amoney;
public Account(String aname,int amoney){
this.aname=aname;
this.amoney=amoney;
}
}
二、Serializable
一个对象序列化的接口,一个类只有实现了Serializable接口,它的对象才能被序列化。
序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。
三、dbUtil-阿帕奇提供操作数据库的插件
1. 依赖:
2. 数据源和QuerryRunner注入(applicationContext.html)
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 注入数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${msg1}"/>
<property name="jdbcUrl" value="${msg2}"/>
<property name="user" value="${msg3}"/>
<property name="password" value="${msg4}"/>
</bean>
<!-- 注入QueryRunner -->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"/>
</bean>
<!-- 注入dao -->
<bean id="mapperImp" class="com.apesource.dao.AccountMapperImp">
<property name="queryRunner" ref="queryRunner"/>
</bean>
<!-- 注入service -->
<bean id="service" class="com.apesource.service.AccountServiceImp">
<property name="mapper" ref="mapperImp"/>
</bean>
<!-- 注入controller -->
<bean id="controller" class="com.apesource.controller.AccountControllerImp">
<property name="service" ref="service"/>
</bean>
</beans>
3. 核心类:QueryRunner
4. QueryRunner提供的方法:
(1)query() 查询
BeanHandler:把结果集转为一个 Bean,并返回。Bean的类型在创建BeanHandler 对象时以 Class 对象的方式传入 BeanHandler(Class<T> type)。
BeanListHandler:把结果集转为一个 Bean 的 List, 并返回。Bean的类型在创建 BeanListHanlder对象时以 Class对象的方式传入BeanListHandler(Class<T> type)。
(2)update() 增删改
package com.apesource.dao;
import com.apesource.pojo.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class AccountMapperImp implements IAccountMapper {
// 操作数据库的核心类
QueryRunner queryRunner;
public void setQueryRunner(QueryRunner queryRunner) {
this.queryRunner = queryRunner;
}
@Override
public void save(Account account) {
try {
queryRunner.update("insert into account(aname,amoney) values(?,?)",account.getAname(),account.getAmoney());
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
@Override
public void deleteById(int id) {
try {
queryRunner.update("delete from account where aid =?",id);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
@Override
public void updateById(Account account) {
try {
queryRunner.update("update account set aname=?,amoney=? where aid=?",account.getAname(),account.getAmoney(),account.getAid());
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
@Override
public Account findByName(String name) {
try {
return queryRunner.query("select * from account where aname=?",new BeanHandler<Account>(Account.class),name);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
@Override
public List<Account> findAll() {
try {
return queryRunner.query("select * from account",new BeanListHandler<Account>(Account.class));
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
}
四、junit测试
1. 使用步骤:
(1)坐标(依赖)
(2)注解
修饰方法:
@Test======>可以运行的方法
@Before====>@Test运行之前
@After=====>@Test运行之后
基于xml实现:
package com.apesource.test;
import com.apesource.controller.IAccountController;
import com.apesource.pojo.Account;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test01 {
ClassPathXmlApplicationContext applicationContext = null;
IAccountController controller = null;
@Before // 测试运行前执行
public void beforeMethod(){
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
controller = (IAccountController) applicationContext.getBean("controller");
}
@After // 测试执行后执行
public void afterMethod(){
applicationContext.close(); // 关闭容器
}
@Test
public void show3(){
controller.save(new Account("王五",7000));
}
@Test
public void show4(){
List<Account> all = controller.findAll();
for (int i = 0; i < all.size(); i++) {
Account account = all.get(i);
System.out.println(account);
}
}
}
修饰类:
@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境,搭配@ContextConfiguration 使用,Spring整合JUnit4测试时,使用注解引入多个配置文件。
基于annotation注解实现:
package com.apesource.test;
import com.apesource.controller.IAccountController;
import com.apesource.pojo.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test02 {
@Autowired
IAccountController controller;
@Test
public void show1(){
controller.save(new Account("小陈",6000));
}
@Test
public void show2(){
List<Account> all = controller.findAll();
for (int i = 0; i < all.size(); i++) {
Account account = all.get(i);
System.out.println(account);
}
}
@Test
public void show3(){
Account account = new Account();
account.setAid(4);
account.setAname("辰辰");
account.setAmoney(8000);
controller.updateById(account);
}
}