恶心啊,至始至终对编程都不感冒,没办法,专业也是自己选的,不学能咋地,像别人那样,混日子,我也做不到。
因为我缺钱啊,我需要搞钱,需要吃饭。
学吧,就这样死啃吧。
时间也不多了。
一切为了生存!!!
自己还是鱼的记忆,今天学的明天忘,以后博客是不能断了........
在软件设计当中,为了实现软件的高内聚低耦合,会将整个代码的结构划分为,表现层,业务层,数据访问层的三层架构。
关于三层架构的理解:可以和生活练习起来:
可以借鉴服务员,厨师,采购员之间的关系:
顾客直接和服务员打交道,顾客和服务员(UI层)说:我要一个炒茄子,而服务员不负责炒茄子,她就把请求往上递交,传递给厨师(BLL层),厨师需要茄子,就把请求往上递交,传递给采购员(DAL层),采购员从仓库里取来茄子传回给厨师,厨师响应cookEggplant()方法,做好炒茄子后,又传回给服务员,服务员把茄子呈现给顾客。
UI(表现层): 主要是指与用户交互的界面。用于接收用户输入的数据和显示处理后用户需要的数据。
BLL:(业务逻辑层): UI层和DAL层之间的桥梁。实现业务逻辑。业务逻辑具体包含:验证、计算、业务规则等等。
DAL:(数据访问层): 与数据库打交道。主要实现对数据的增、删、改、查。将存储在数据库中的数据提交给业务层,同时将业务层处理的数据保存到数据库。(当然这些操作都是基于UI层的。用户的需求反映给界面(UI),UI反映给BLL,BLL反映给DAL,DAL进行数据的操作,操作后再一一返回,直到将用户所需数据反馈给用户)
简言之:
- 表现层(View):收集用户名密码,然后展示。
- 业务层(Service):将拿来的数据进行加工和校验。
- 数据访问层(DAO):负责数据库访问。
conf 用来配置文件
dao 数据访问对象接口 数据访问层
数据访问对象接口实现类
entity 实体对象
service 业务层接口 业务层
业务层接口实现类
sql 项目相关的sql命令文件
util 工具类
view 表现层代码 视图层
orm的简单测试
1引入sql
CREATE TABLE ACCOUNT(
cardid INT(7) PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(15) NOT NULL,
PASSWORD VARCHAR(6) NOT NULL,
balance double
);
INSERT INTO ACCOUNT(NAME,PASSWORD,balance) VALUES('蒋静','123456',500);
INSERT INTO ACCOUNT(NAME,PASSWORD,balance) VALUES('徐成','123456',800);
SELECT * FROM ACCOUNT;
2.创建字段的实体。shift+alt+s快捷键
package entity;
public class Account {
private Integer cardid;
private String name;
private String password;
private double balance;
public Account() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Accout [cardid=" + cardid + ", name=" + name + ", password=" + password + ", balance=" + balance + "]";
}
public Account(Integer cardid, String name, String password, double balance) {
super();
this.cardid = cardid;
this.name = name;
this.password = password;
this.balance = balance;
}
public Integer getCardid() {
return cardid;
}
public void setCardid(Integer cardid) {
this.cardid = cardid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
3.测试
package Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import entity.Account;
import util.jdbcUtil3;
public class TestOrm {
@Test
public void testQuery() throws Exception{
Connection conn = jdbcUtil3.getConnection();
String sql = "select * from account where name='lb'";
PreparedStatement pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
//创建Account对象,对应查询的一条记录
Account account = new Account();
//处理结果集
if(rs.next()){
int cardid = rs.getInt(1);
String name= rs.getString(2);
String password = rs.getString(3);
double balance =rs.getDouble(4);
//查询的记录。给Account对象赋值
account.setCardid(cardid);
account.setName(name);
account.setPassword(password);
account.setBalance(balance);
}
System.out.println(account);
}
@Test
public void testQueryAll() throws Exception{
Connection conn = jdbcUtil3.getConnection();
String sql ="select * from account";
PreparedStatement pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
//创建Account集合,对应多条记录
List list = new ArrayList();
while(rs.next()){
int cardid = rs.getInt(1);
String name= rs.getString(2);
String password = rs.getString(3);
double balance =rs.getDouble(4);
//将查询的一条记录封装到一个Accout对象中
Account account = new Account();
account.setCardid(cardid);
account.setName(name);
account.setPassword(password);
account.setBalance(balance);
list.add(account);
}
//遍历list
for(Account account : list)
{
System.out.println(account);
}
}
}
测试结果成功了,但是可以发现,查询部分有很多重复的代码,代码利用率不高,所以有必要进行封装了‘’
新建个接口,在整个继承接口的类,把查询数据那步集成一下
package rowmapper;
import java.sql.ResultSet;
public interface RowMapper {
//将ResultSet结果集封装成对象
public T mappreRow(ResultSet rs);
}
package rowmapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import entity.Account;
import rowmapper.RowMapper;
public class AccountRowMapper implements RowMapper{
@Override
public Object mappreRow(ResultSet rs) {
// TODO Auto-generated method stub
Account account = new Account();
try {
account.setCardid(rs.getInt(1));
account.setName(rs.getString("name"));
account.setPassword(rs.getString(3));
account.setBalance(rs.getDouble(4));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return account;
}
}
package Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import entity.Account;
import rowmapper.AccountRowMapper;
import rowmapper.RowMapper;
import util.jdbcUtil3;
public class TestOrm {
RowMapper rm = new AccountRowMapper();
@Test
public void testQuery() throws Exception{
Connection conn = jdbcUtil3.getConnection();
String sql = "select * from account where name='wxd'";
PreparedStatement pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
//创建Account对象,对应查询的一条记录
Account account = new Account();
//处理结果集
if(rs.next()){
account = (Account) rm.mappreRow(rs);
}
System.out.println(account);
}
@Test
public void testQueryAll() throws Exception{
Connection conn = jdbcUtil3.getConnection();
String sql ="select * from account";
PreparedStatement pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
//创建Account集合,对应多条记录
List list = new ArrayList();
while(rs.next()){
Account account = (Account) rm.mappreRow(rs);
list.add(account);
}
//遍历list
for(Account account : list)
{
System.out.println(account);
}
}
}