org.apache.struts
struts2-core
2.3.35
org.apache.struts
struts2-spring-plugin
2.3.35
org.apache.struts
struts2-json-plugin
2.3.8
1.4 配置Java EE 坐標依賴
這里可以引入 servlet api,jstl 標簽庫等一系列工具
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
org.projectlombok
lombok
1.18.0
provided
jstl
jstl
1.2
taglibs
standard
1.1.2
1.5 其他工具
json 處理工具
org.jetbrains
annotations-java5
RELEASE
compile
org.json
json
20160810
=======================================================================
2.1 配置文件
使用如下方式創建
-
applicationContext.xml
-
jdbc.properties
-
struts.xml
2.2 包結構
創建如下的基本包結構
=======================================================================
3.1 web.xml 文件配置
Archetype Created Web Application
contextConfigLocation
classpath:applicationContext.xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
org.springframework.web.context.ContextLoaderListener
3.2 編寫 jdbc.properties 文件
這里我們需要自己手動修改數據庫的信息配置
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf-8&autoReconnect=true&useSSL=false
jdbc.user=root
jdbc.password=root
#連接池中保留的最小連接數
jdbc.minPoolSize=1
#連接池中保留的最大連接數
jdbc.maxPoolSize=20
#初始化連接數
jdbc.initialPoolSize=1
3.3 編寫 applicationContext.xml 配置文件
這里面也包含了數據庫的基本配置
<?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:tx=“http://www.springframework.org/schema/tx”
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location=“classpath:jdbc.properties”/>
<context:component-scan base-package=“dao.,service.”/>
<context:component-scan base-package=“action”/>
context:annotation-config/
org.hibernate.dialect.MySQLDialect
update
thread
true
true
false
<tx:annotation-driven transaction-manager=“txManager”/>
3.4 struts 配置文件
我們還沒有編寫的具體的 action 服務,所以這里先跳過
========================================================================================
4.1 配置數據庫連接信息
使用 idea 自帶的數據庫連接的工具
完善基本配置信息
4.2 逆向生成實體類
4.3 實體類配置
生成好后可以看到和數據庫對應的實體類,我的表很簡單,一個簡單的用戶表,只有 id, username, password 字段
但是我們發現里面的部分內容會爆紅,這是因為我們沒有指定數據源
選擇我們剛才連接的數據庫
然后就沒問題了。
============================================================================
看到包結構,大家應該可以猜出來,我是使用的典型的 MVC 三層架構來編寫的
5.1 編寫 dao 層
創建 UserDao 以及 它的實現類 UserDaoImpl
UserDao 編寫
package dao;
import entity.User;
public interface UserDao {
// 用戶登錄驗證
public User selectByUsernameAndPassword(String username, String password);
}
UserDaoImpl
package dao.Impl;
import dao.UserDao;
import entity.User;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
// 使用 Spring 來接管持久層的所有操作
@Repository
public class UserDaoImpl implements UserDao {
// 使用 Hibernate 提供的模板
@Autowired
@Resource
private HibernateTemplate hibernateTemplate;
// 生成對應的 get 和 set 方法
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public User selectByUsernameAndPassword(String username, String password) {
// 登錄的邏輯不算難,就是使用 sql 語句查詢,username 和 password 兩個字段是否存在即可,我們使用的是 hibernate 框架,所以要寫 hql 語句
Session session = hibernateTemplate.getSessionFactory().openSession();
Query q = session.createQuery(“from User u where u.username = ? and u.password = ?”);
q.setParameter(0,username);
q.setParameter(1,password);
User u = (User) q.uniqueResult();
return u;
}
}
我們寫好了 dao 層,這時候發現出現了爆紅的問題,這里我們需要手動添加項目的依賴信息
點擊 project structure
添加這個就可以了,問題就解決了
顯示正常了
5.2 編寫 Service 層
同樣,我們創建對應的 UserService 和 對應的 UserServiceImpl 類
有的同學可能會問道,不就是一個簡單的登錄功能嘛,有必要這么麻煩嗎?是的,這么做確實沒必要,但是隨著項目的越來越大,只有把具體的功能全部分開來做,這樣才不至于整個項目太過于亂
編寫用戶的業務層 接口 UserService
package service;
import entity.User;
public interface UserService {
// 登錄驗證
User checklogin(String username, String password);
}
編寫 業務層對應的實現類 UserServiceImpl
package service.Impl;
import dao.UserDao;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.UserService;
@Service
public class UserServiceImpl implements UserService {
// 這里業務層調用持久層的方法
@Autowired
private UserDao ud;
@Override
public User checklogin(String username, String password) {
return ud.selectByUsernameAndPassword(username,password);
}
}
5.3 編寫 Controller 層 (UserAction)
這里的邏輯思路,是 controller 層 調用 service 的方法,service 層調用 dao 層的方法
package action;
import com.opensymphony.xwork2.ActionContext;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import service.UserService;
import java.util.Map;
// 使用 Controller 表示這是控制層,使用 ua 表示這個類被 Spring 所管理
@Controller(“ua”)
public class UserAction {
// 編寫兩個屬性,使用 struts2 的 ognl 表達式可以直接接收到前端穿過來的數據,不再需要 request.getParameter(“xxxx”) 接收數據了
private String username;
private String password;
// 調用業務層的方法
@Autowired
private UserService us;
// get 方法可以不要, set 方法必須有,不然前端的數據就無法注入進來
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// 編寫登錄逇控制層方法
public String login() {
System.out.println(username + " " + password); // 打印穿過來的數據
ActionContext ac = ActionContext.getContext();
// 得到 servlet 中的三大域的 session 域,在這里我們要將數據保存至 session,并在前端展示
Map<String,Object> session = ac.getSession(); // 我們可以看到 session 的實質就是一個 map
User user = us.checklogin(username,password); // 登錄驗證
if ( user!=null ) {
session.put(“user”,username);
return “success”;
} else {
return “error”;
}
}
}
5.4 編寫 struts 路由映射
記得在 Project Structure 添加如下配置
stucts action 配置
<?xml version="1.0" encoding="UTF-8"?>自我介紹一下,小編13年上海交大畢業,曾經在小公司待過,也去過華為、OPPO等大廠,18年進入阿里一直到現在。
深知大多數Java工程師,想要提升技能,往往是自己摸索成長或者是報班學習,但對于培訓機構動則幾千的學費,著實壓力不小。自己不成體系的自學效果低效又漫長,而且極易碰到天花板技術停滯不前!
因此收集整理了一份《2024年Java開發全套學習資料》,初衷也很簡單,就是希望能夠幫助到想自學提升又不知道該從何學起的朋友,同時減輕大家的負擔。
既有適合小白學習的零基礎資料,也有適合3年以上經驗的小伙伴深入學習提升的進階課程,基本涵蓋了95%以上Java開發知識點,真正體系化!
由于文件比較大,這里只是將部分目錄截圖出來,每個節點里面都包含大廠面經、學習筆記、源碼講義、實戰項目、講解視頻,并且會持續更新!
如果你覺得這些內容對你有幫助,可以掃碼獲取!!(備注Java獲取)

總結
我們總是喜歡瞻仰大廠的大神們,但實際上大神也不過凡人,與菜鳥程序員相比,也就多花了幾分心思,如果你再不努力,差距也只會越來越大。實際上,作為程序員,豐富自己的知識儲備,提升自己的知識深度和廣度是很有必要的。
Mybatis源碼解析
《互聯網大廠面試真題解析、進階開發核心學習筆記、全套講解視頻、實戰項目源碼講義》點擊傳送門即可獲取!
技能,往往是自己摸索成長或者是報班學習,但對于培訓機構動則幾千的學費,著實壓力不小。自己不成體系的自學效果低效又漫長,而且極易碰到天花板技術停滯不前!**
因此收集整理了一份《2024年Java開發全套學習資料》,初衷也很簡單,就是希望能夠幫助到想自學提升又不知道該從何學起的朋友,同時減輕大家的負擔。[外鏈圖片轉存中…(img-C1M1t1tm-1713384424162)]
[外鏈圖片轉存中…(img-VMFfQBTB-1713384424162)]
[外鏈圖片轉存中…(img-AN1CZh1T-1713384424163)]
既有適合小白學習的零基礎資料,也有適合3年以上經驗的小伙伴深入學習提升的進階課程,基本涵蓋了95%以上Java開發知識點,真正體系化!
由于文件比較大,這里只是將部分目錄截圖出來,每個節點里面都包含大廠面經、學習筆記、源碼講義、實戰項目、講解視頻,并且會持續更新!
如果你覺得這些內容對你有幫助,可以掃碼獲取!!(備注Java獲取)

總結
我們總是喜歡瞻仰大廠的大神們,但實際上大神也不過凡人,與菜鳥程序員相比,也就多花了幾分心思,如果你再不努力,差距也只會越來越大。實際上,作為程序員,豐富自己的知識儲備,提升自己的知識深度和廣度是很有必要的。
Mybatis源碼解析
[外鏈圖片轉存中…(img-7W9TCuse-1713384424163)]
[外鏈圖片轉存中…(img-mJri2T8N-1713384424163)]
《互聯網大廠面試真題解析、進階開發核心學習筆記、全套講解視頻、實戰項目源碼講義》點擊傳送門即可獲取!