网站过期后,瓯海网站建设,商户网站唯一订单号,如何制作手机购物网站一、lombok插件
1. 功能#xff1a;对实体类自动#xff0c;动态生成get、set方法#xff0c;无参、有参构造.....
2. 步骤#xff1a; #xff08;1#xff09;idea安装插件(只做一次) #xff08;2#xff09;添加坐标 #xff08;3#xff09;编写注解 NoArgsCo…一、lombok插件
1. 功能对实体类自动动态生成get、set方法无参、有参构造.....
2. 步骤 1idea安装插件(只做一次) 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.anameaname;this.amoneyamoney;}
}二、Serializable 一个对象序列化的接口一个类只有实现了Serializable接口它的对象才能被序列化。 序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化它将流转换为对象。这两个过程结合起来可以轻松地存储和传输数据。 三、dbUtil-阿帕奇提供操作数据库的插件
1. 依赖 2. 数据源和QuerryRunner注入applicationContext.html
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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 locationclasspath:jdbc.properties/!-- 注入数据源 --bean iddataSource classcom.mchange.v2.c3p0.ComboPooledDataSourceproperty namedriverClass value${msg1}/property namejdbcUrl value${msg2}/property nameuser value${msg3}/property namepassword value${msg4}//bean!-- 注入QueryRunner --bean idqueryRunner classorg.apache.commons.dbutils.QueryRunnerconstructor-arg nameds refdataSource//bean!-- 注入dao --bean idmapperImp classcom.apesource.dao.AccountMapperImpproperty namequeryRunner refqueryRunner//bean!-- 注入service --bean idservice classcom.apesource.service.AccountServiceImpproperty namemapper refmapperImp//bean!-- 注入controller --bean idcontroller classcom.apesource.controller.AccountControllerImpproperty nameservice refservice//bean/beans
3. 核心类QueryRunner
4. QueryRunner提供的方法 1query() 查询 BeanHandler把结果集转为一个 Bean并返回。Bean的类型在创建BeanHandler 对象时以 Class 对象的方式传入 BeanHandler(ClassT type)。 BeanListHandler把结果集转为一个 Bean 的 List, 并返回。Bean的类型在创建 BeanListHanlder对象时以 Class对象的方式传入BeanListHandler(ClassT type)。 2update() 增删改
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;}Overridepublic void save(Account account) {try {queryRunner.update(insert into account(aname,amoney) values(?,?),account.getAname(),account.getAmoney());} catch (SQLException throwables) {throwables.printStackTrace();}}Overridepublic void deleteById(int id) {try {queryRunner.update(delete from account where aid ?,id);} catch (SQLException throwables) {throwables.printStackTrace();}}Overridepublic 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();}}Overridepublic Account findByName(String name) {try {return queryRunner.query(select * from account where aname?,new BeanHandlerAccount(Account.class),name);} catch (SQLException throwables) {throwables.printStackTrace();}return null;}Overridepublic ListAccount findAll() {try {return queryRunner.query(select * from account,new BeanListHandlerAccount(Account.class));} catch (SQLException throwables) {throwables.printStackTrace();}return null;}
}四、junit测试
1. 使用步骤 1坐标依赖 2注解 修饰方法 Test可以运行的方法 BeforeTest运行之前 AfterTest运行之后 基于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(); // 关闭容器}Testpublic void show3(){controller.save(new Account(王五,7000));}Testpublic void show4(){ListAccount 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 {AutowiredIAccountController controller;Testpublic void show1(){controller.save(new Account(小陈,6000));}Testpublic void show2(){ListAccount all controller.findAll();for (int i 0; i all.size(); i) {Account account all.get(i);System.out.println(account);}}Testpublic void show3(){Account account new Account();account.setAid(4);account.setAname(辰辰);account.setAmoney(8000);controller.updateById(account);}}