使用maven构建多模块项目(三)

时间:2023-01-21 11:26:18
  1. 完善helloweb-entity模块
  2. 完善helloweb-core模块
  3. 完善helloweb-web模块
  4. 运行

1. 完善helloweb-entity模块

(1)创建数据表

CREATE TABLE `tbl_user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;


插入一条数据

insert into tbl_user(name, password, email) values ('Tom', '123456','Tom@qq.com');

(2)创建实体类User

package com.jikexueyuan.entity;

public class User {
private long id;
private String name;
private String password;
private String email;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

2. 完善helloweb-core模块

(1)新建一个resources文件夹,在main下,右击,Build Path, Use as Source Folder

新建dbconfig.properties

driver=com.mysql.jdbc.Driver
dburl=jdbc:mysql://localhost:3306/ssm
user=root
password=root

(2)新建一个类ConnectionFactory

package com.jikexueyuan.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionFactory {

private static String driver;
private static String dburl;
private static String user;
private static String password;

private static final ConnectionFactory factory = new ConnectionFactory();
private Connection conn;

static{
Properties prop = new Properties();
try {
InputStream is = ConnectionFactory.class.getClassLoader().getResourceAsStream("dbconfig.properties");
prop.load(is);
} catch (Exception e) {
System.out.println("获取数据库配置文件错误"+e.getMessage());
}

driver = prop.getProperty("driver");
dburl = prop.getProperty("dburl");
user = prop.getProperty("user");
password = prop.getProperty("password");
}

//测试
private ConnectionFactory(){

}

//单例模式
public static ConnectionFactory getInstance(){
return factory;
}

//定义一个用于获取连接的方法
public Connection makeConnection(){
try {
Class.forName(driver);
conn = DriverManager.getConnection(dburl, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

//测试
public static void main(String[] args) throws SQLException {
Connection conn = ConnectionFactory.getInstance().makeConnection();
System.out.println(conn.getAutoCommit());
}

}

(3)发现缺少mysql驱动,在helloweb-core——>pom.xml中添加,复制helloweb-parent下pom.xml中的mysql驱动的dependency,删除version

(4)运行ConnectionFactory进行测试
(5)在com.jikexueyuan.dao下创建类UserDao

package com.jikexueyuan.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.jikexueyuan.entity.User;

public interface UserDao {

public void save(Connection conn, User user) throws SQLException;

public void update(Connection conn, Long id, User user) throws SQLException;

public void delete(Connection conn, User user) throws SQLException;

public ResultSet get(Connection conn, User user) throws SQLException;
}

(6)在com.jikexueyuan.dao.impl下创建类UserDaoImpl

package com.jikexueyuan.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.jikexueyuan.dao.UserDao;
import com.jikexueyuan.entity.User;

public class UserDaoImpl implements UserDao{

@Override
public void save(Connection conn, User user) throws SQLException {
PreparedStatement ps = conn.prepareCall("INSERT INTO tbl_user(NAME, PASSWORD, EMAIL) VALUES (?,?,?)");

ps.setString(1, user.getName());
ps.setString(2, user.getPassword());
ps.setString(3, user.getEmail());
ps.execute();
}

@Override
public void update(Connection conn, Long id, User user) throws SQLException {
String updateSql = "update tbl_user set name = ?, password = ?, email = ? where id = ?";
PreparedStatement ps = conn.prepareStatement(updateSql);

ps.setString(1, user.getName());
ps.setString(2, user.getPassword());
ps.setString(3, user.getEmail());
ps.setLong(4, id);

ps.execute();
}

@Override
public void delete(Connection conn, User user) throws SQLException {
PreparedStatement ps = conn.prepareStatement("delete from tbl_user where id = ?");
ps.setLong(1, user.getId());
ps.execute();
}

@Override
public ResultSet get(Connection conn, User user) throws SQLException {
PreparedStatement ps = conn.prepareStatement("select * from tbl_user where name = ? and password =?");
ps.setString(1, user.getName());
ps.setString(2, user.getPassword());

return ps.executeQuery();
}


}

(7)在com.jikexueyuan.service下创建类CheckUserService

package com.jikexueyuan.service;

import java.sql.Connection;
import java.sql.ResultSet;

import com.jikexueyuan.dao.UserDao;
import com.jikexueyuan.dao.impl.UserDaoImpl;
import com.jikexueyuan.entity.User;
import com.jikexueyuan.util.ConnectionFactory;

public class CheckUserService {

private UserDao userDao = new UserDaoImpl();

public boolean check(User user){
Connection conn = null;

try{
conn = ConnectionFactory.getInstance().makeConnection();
conn.setAutoCommit(false);

ResultSet resultSet = userDao.get(conn, user);
while(resultSet.next()){
return true;
}
}catch(Exception e){
e.printStackTrace();
try {
conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
}
}finally {
try{
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
}

(8)需要添加helloweb-entity包下的User依赖,内容如下:

<dependency>
<groupId>com.moss</groupId>
<artifactId>helloweb-entity</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>

(9)打包项目maven install

3. 完善helloweb-web模块

(1)在com.jikexueyuan.action包下创建CheckAction类

package com.jikexueyuan.action;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jikexueyuan.entity.User;
import com.jikexueyuan.service.CheckUserService;

public class CheckUserAction extends HttpServlet{

private CheckUserService cku = new CheckUserService();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String uname = req.getParameter("uname");
String password = req.getParameter("upwd");
RequestDispatcher rd = null;
String forword = null;

if(uname==null || password==null){
req.setAttribute("msg","用户名或密码为空");
rd = req.getRequestDispatcher("error.jsp");
rd.forward(req, resp);
}else{
User user = new User();
user.setName(uname);
user.setPassword(password);
boolean bool = cku.check(user);

if(bool){
forword = "success.jsp";
}else{
req.setAttribute("msg", "用户名或密码错误,请重新输入");
forword = "error.jsp";
}

rd = req.getRequestDispatcher(forword);
rd.forward(req, resp);
}
}
}


4. 运行

使用maven构建多模块项目(三)

点击上图红色框框中的倒三角,选择Run Configurations——>maven build——>new,填写下面信息:
1)Base directory——>Browse Workspace——>选择helloweb-web
2)Goals:tomcat:run
3 )Name:run_helloweb(可改可不改)
4)Apply
5 )Run
Tomcat启动完毕