20210324-javaee-用STS创建maven的quick-start演示项目

按骨架创建项目

1. 选quick-start
2. 调整pom,添加依赖
3. 观察项目结构

一般过程

1. 创建pojo
2. 创建DBUtil
3. 创建测试
	3.1 用main进行测试
	3.2 用junit单元测试

连接数据库

1. 工具类封装
2. 工具类使用,完成CRUD操作
3. 通用DAO设计与实现
4. 专用DAO生成与实现
5. 测试DAO

具体文件参考

pom.xml

<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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.zfh</groupId>
  <artifactId>maven-demo-quick-01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>maven-demo-quick-01</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.38</version>
	</dependency>
	<dependency>
	    <groupId>junit</groupId>
	    <artifactId>junit</artifactId>
	    <version>4.12</version>
	    <scope>test</scope>
	</dependency>
	<dependency>
	    <groupId>org.projectlombok</groupId>
	    <artifactId>lombok</artifactId>
	    <version>1.18.12</version>
	</dependency>
  </dependencies>
</project>

# 注意,这里,生成的是jar包

项目结构

20210324-javaee-用STS创建maven的quick-start演示项目

具体java程序文档

User.java

package com.zfh.entity;

import java.util.Date;

import lombok.Data;

@Data
public class User {

	private String id;
	private String name;
	private String gender;
	private Date bir;
	private Integer score;
}


DAO.java

package com.zfh;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import com.zfh.util.DBUtil;

public class DAO {

	private DBUtil dbUtil = new DBUtil();
	private Connection conn = dbUtil.getConn();
	
	public int updateData(String updateSql, List<Object> params) throws Exception{
		int result = -1;
		PreparedStatement ps = conn.prepareStatement(updateSql);
		for (int i = 0; i < params.size(); i++) {
			ps.setObject(i+1, params.get(i));
		}
		result = ps.executeUpdate();
		return result;
	}
	
	public ResultSet selectData(String selectSql, List<Object> params) throws Exception{
		ResultSet rs = null;
		PreparedStatement ps = conn.prepareStatement(selectSql);
		if(params != null) {
			for (int i = 0; i < params.size(); i++) {
				ps.setObject(i+1, params.get(i));
			}
		}
		rs = ps.executeQuery();
		return rs;
	}
}

UserDAO.java

package com.zfh;

public class UserDAO extends DAO {
	
}

DBUitl.java

package com.zfh.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil {

	private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
	private static final String DB_URL = "jdbc:mysql://localhost:3306/curdtestdb";
	private static final String DB_USER = "root";
	private static final String DB_PWD = "root";
	
	private Connection conn = null;
	
	public DBUtil() {
		try {
			Class.forName(DB_DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PWD);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public Connection getConn() {return conn;}
	
	public void close() {
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

App.java

package com.zfh;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.zfh.entity.User;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
//    	testInsertData();
//    	testUpdateData();
//    	testDeleteData();
    	testeSelectData();
    }
    
    public static void testUpdateData() {
    	UserDAO userDAO = new UserDAO();
        String sql = "update t_user set name = ?, gender = ?,bir = ?, score = ? where id = ?";
        // 两种优雅的方式对list进行赋值
        List<Object> params = new ArrayList<Object>(){
        	{
	        	add("克林丰华");
	        	add("女");
	        	add("2000-10-10");
	        	add(85);
		        	add(12004);
        	}
        };
        List<Object> params2 = Arrays.asList(12005,"皮特丰华","男","1999-09-09",98);
        try {
			int result = userDAO.updateData(sql, params);
			System.out.println("有"+result+"条记录修改");
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public static void testDeleteData() {
    	UserDAO userDAO = new UserDAO();
        String sql = "delete from t_user where id = ?";
        // 两种优雅的方式对list进行赋值
        List<Object> params = new ArrayList<Object>(){
        	{
//	        	add("克林丰华");
//	        	add("女");
//	        	add("2000-10-10");
//	        	add(85);
		        add(12004);
        	}
        };
//        List<Object> params2 = Arrays.asList(12005,"皮特丰华","男","1999-09-09",98);
        try {
			int result = userDAO.updateData(sql, params);
			System.out.println("有"+result+"条记录被删除");
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    public static void testInsertData()
    {
        UserDAO userDAO = new UserDAO();
        String sql = "insert into t_user values(?,?,?,?,?)";
        // 两种优雅的方式对list进行赋值
        List<Object> params = new ArrayList<Object>(){
        	{
        		add(12004);
	        	add("刘丰华");
	        	add("男");
	        	add("2000-10-10");
	        	add(95);
        	}
        };
        List<Object> params2 = Arrays.asList(12005,"皮特丰华","男","1999-09-09",98);
        try {
			int result = userDAO.updateData(sql, params2);
			System.out.println("有"+result+"条记录入库");
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public static void testeSelectData() {
    	UserDAO userDAO = new UserDAO();
        String sql = "select * from t_user";
        List<User> users = new ArrayList<>();
        try {
			ResultSet rs = userDAO.selectData(sql, null);
			while (rs.next()) {
				User u = new User();
				u.setId(rs.getString(1));
				u.setName(rs.getString(2));
				u.setGender(rs.getString(3));
				u.setBir(rs.getDate(4));
				u.setScore(rs.getInt(5));
				users.add(u);
			}
			System.out.println("有"+users.size()+"条记录被查询到");
//			System.out.println(users);
			for (User user : users) {
				System.out.println(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}

AppTest.java

package com.zfh;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

测试时注意事项

  1. 观察结果的位置:控制台
  2. main可以跑,junit也可以跑
  3. 适当设置开关,进行异常检查

项目演示完后,写后记

上一篇:老韩一周学会Linux——笔记整理目录(超级详细)——更新至JAVAEE篇


下一篇:JavaEE简介