http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:property-placeholder location=“classpath:db.properties” />
<!–JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。
所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:
0 -->
<!–获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。
如果设为true,那么在尝试 获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
<!–因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod
或automaticTestTable 等方法来提升连接测试的性能。Default: false -->
<bean id=“sessionFactory”
class=“org.springframework.orm.hibernate5.LocalSessionFactoryBean”>
org.hibernate.dialect.MySQLDialect
true
true
com/lv/book/entity/Book.hbm.xml
<!–
静态代理:
一个代理对象->一个目标对象
BookProxy(BookBizImpl+myMethodBeforeAdvice)->bookBiz
OrderProxy(OrderBizImpl+myMethodBeforeAdvice2)-> OrderBiz
动态代理:
一个代理对象->多个目标对象
–>
<aop:aspectj-autoproxy />
<bean id=“transactionManager”
class=“org.springframework.orm.hibernate5.HibernateTransactionManager”>
<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
tx:attributes
<tx:method name=“add*” propagation=“REQUIRED” />
<tx:method name=“save*” propagation=“REQUIRED” />
<tx:method name=“insert*” propagation=“REQUIRED” />
<tx:method name=“edit*” propagation=“REQUIRED” />
<tx:method name=“update*” propagation=“REQUIRED” />
<tx:method name=“del*” propagation=“REQUIRED” />
<tx:method name=“remove*” propagation=“REQUIRED” />
<tx:method name=“load*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“list*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“select*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“query*” propagation=“REQUIRED” read-only=“true” />
<tx:method name=“do*” propagation=“REQUIRED” />
</tx:attributes>
</tx:advice>
aop:config
<aop:advisor advice-ref=“txAdvice” pointcut=“execution(* *…*Biz.*(…))” />
</aop:config>
6、struts配置文件struts.xml
<?xml version="1.0" encoding="UTF-8"?>
“-//Apache Software Foundation//DTD Struts Configuration 2.5//EN”
“http://struts.apache.org/dtds/struts-2.5.dtd”>
7、struts的基础板块配置:struts-base.xml
<?xml version="1.0" encoding="UTF-8"?>
“-//Apache Software Foundation//DTD Struts Configuration 2.5//EN”
“http://struts.apache.org/dtds/struts-2.5.dtd”>
regex:.*
8、具体模块的某个功能:struts-sy.xml
<?xml version="1.0" encoding="UTF-8"?>
“-//Apache Software Foundation//DTD Struts Configuration 2.5//EN”
“http://struts.apache.org/dtds/struts-2.5.dtd”>
/add.jsp
/index.jsp
9、web.xml配置
<web-app xmlns=“http://xmlns.jcp.org/xml/ns/javaee”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd”
version=“3.1”>
Archetype Created Web Application
contextConfigLocation
classpath:spring-context.xml
org.springframework.web.context.ContextLoaderListener
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
二、演示所需的类
1、基础板块的类BaseEntity:
package com.lv.base.entity;
import java.io.Serializable;
public class BaseEntity implements Serializable{
private static final long serialVersionUID = -5475556860252527343L;
}
2、分页PageBean:
package com.lv.base.util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
/**
* 分页工具�?
*
*/
public class PageBean {
private int page = 1;// 页码
private int rows = 3;// 页大�?
private int total = 0;// 总记录数
private boolean pagination = true;// 是否分页
private String url; //保存上一次请求的URL
private Map<String,String[]> paramMap = new HashMap<>();// 保存上一次请求的参数
/**
* 初始化pagebean的,保存上一次请求的重要参数
* @param req
*/
public void setRequest(HttpServletRequest req) {
// 1.1 �?要保存上�?次请求的URL
this.setUrl(req.getRequestURL().toString());
// 1.2 �?要保存上�?次请求的参数 bname、price
this.setParamMap(req.getParameterMap());
// 1.3 �?要保存上�?次请求的分页设置 pagination
this.setPagination(req.getParameter(“pagination”));
// 1.4 �?要保存上�?次请求的展示条目�?
this.setRows(req.getParameter(“rows”));
// 1.5 初始化请求的页码 page
this.setPage(req.getParameter(“page”));
}
public void setPage(String page) {
if(StringUtils.isNotBlank(page))
this.setPage(Integer.valueOf(page));
}
public void setRows(String rows) {
if(StringUtils.isNotBlank(rows))
this.setRows(Integer.valueOf(rows));
}
public void setPagination(String pagination) {
// 只有在前台jsp填写了pagination=false,才代表不分�?
if(StringUtils.isNotBlank(pagination))
this.setPagination(!“false”.equals(pagination));
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map<String, String[]> getParamMap() {
return paramMap;
}
public void setParamMap(Map<String, String[]> paramMap) {
this.paramMap = paramMap;
}
public PageBean() {
super();
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return pagination;
}
public void setPagination(boolean pagination) {
this.pagination = pagination;
}
/**
* 获得起始记录的下�?
*
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
/**
* �?大页
* @return
*/
public int maxPage() {
// total % rows == 0 ? total / rows : total / rows +1
return this.total % this.rows == 0 ? this.total / this.rows : this.total / this.rows + 1;
}
/**
* 下一�?
* @return
*/
public int nextPage() {
// 如果当前页小于最大页,那就下�?页为当前�?+1;如果不小于,说明当前页就是�?大页,那就无�?+1
return this.page < this.maxPage() ? this.page + 1 : this.page;
}
/**
* 上一�?
* @return
*/
public int previousPage() {
return this.page > 1 ? this.page - 1 : this.page;
}
@Override
public String toString() {
return “PageBean [page=” + page + “, rows=” + rows + “, total=” + total + “, pagination=” + pagination + “]”;
}
}
3、StringUtils
package com.lv.base.util;
public class StringUtils {
// 私有的构造方法,保护此类不能在外部实例化
private StringUtils() {
}
/**
* 如果字符串等于null或去空格后等�?“”,则返回true,否则返回false
*
* @param s
* @return
*/
public static boolean isBlank(String s) {
boolean b = false;
if (null == s || s.trim().equals(“”)) {
b = true;
}
return b;
}
/**
* 如果字符串不等于null或去空格后不等于"",则返回true,否则返回false
*
* @param s
* @return
*/
public static boolean isNotBlank(String s) {
return !isBlank(s);
}
}
4、BaseDao
package com.lv.base.dao;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.lv.base.util.PageBean;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
public class BaseDao extends HibernateDaoSupport implements Serializable{
private static final long serialVersionUID = 5427369434249665701L;
public void setParam(Query query,Map<String, Object> map) {
if(map == null || map.size() < 1) {
return;
}
Object value = null;
for(Map.Entry<String, Object> entry:map.entrySet()) {
value = entry.getValue();
if(value instanceof Collection) {
query.setParameterList(entry.getKey(), (Collection) value);
}else if(value instanceof Object[]) {
query.setParameterList(entry.getKey(), (Object[]) value);
}else {
query.setParameter(entry.getKey(), value);
}
}
}
public String getCountHql(String hql) {
int index = hql.toUpperCase().indexOf(“FROM”);
return "select count(*) "+hql.substring(index);
}
/**
*
* @param hql 已经拼装好的
* @param map
* @param pageBean
* @return
*/
public List executeQuery(Session session, String hql,Map<String, Object> map,PageBean pageBean) {
if(pageBean !=null && pageBean.isPagination()) {
String countHql = getCountHql(hql);
Query countQuery = session.createQuery(countHql);
this.setParam(countQuery, map);
pageBean.setTotal(countQuery.getSingleResult().toString());
Query query = session.createQuery(hql);
this.setParam(query, map);
query.setFirstResult(pageBean.getStartIndex());
query.setMaxResults(pageBean.getRows());
List list = query.list();
return list;
}else {
Query query = session.createQuery(hql);
this.setParam(query, map);
List list = query.list();
return list;
}
}
}
5、定义接口BaseBiz
package com.lv.base.biz;
public interface BaseBiz {
}
6、BaseAction
package com.lv.base.web;
import java.io.Serializable;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
public class BaseAction implements ServletRequestAware, ServletResponseAware,Serializable{
private static final long serialVersionUID = -7110462505161900879L;
/**
* 为了传值使用
*/
protected HttpServletResponse response;
protected HttpServletRequest request;
protected HttpSession session;
protected ServletContext application;
/**
* 为了配置跳转页面所用
*/
protected final static String SUCCESS = “success”;
protected final static String FAIL = “fail”;
protected final static String LIST = “list”;
protected final static String ADD = “add”;
protected final static String EDIT = “edit”;
protected final static String DETAIL = “detail”;
/**
* 具体传值字段 后端向jsp页面传值所用字段
*/
protected Object result;
protected Object msg;
protected int code;
public Object getResult() {
return result;
}
public Object getMsg() {
return msg;
}
public int getCode() {
return code;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response = arg0;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
this.session = arg0.getSession();
this.application = arg0.getServletContext();
}
}
三、测试
(1、将表描述成实体类,配置相关的映射文件
1、Book
package com.lv.book.entity;
import com.lv.base.entity.BaseEntity;
public class Book extends BaseEntity{
private Integer bid;
private String bname;
private Float price;
public Integer getBid() {
return bid;
}
public void setBid(Integer bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
@Override
public String toString() {
return “Book [bid=” + bid + “, bname=” + bname + “, price=” + price + “]”;
}
}
2、映射文件Book.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
“-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd”>
(2、dao层
package com.lv.book.dao;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;
import com.lv.base.dao.BaseDao;
import com.lv.book.entity.Book;
/**
* 注意:
* 1、一定要继承BaseDao(是为了获取到hibernateTemplete)
* 2、方法命名有讲究(一定要符合声明式事务所定义的规则)
* 若是不遵循规则,那么数据是不会提交到数据库
* 3、hibernateTemplete模板在执行查询的时候比较特殊
* @author Administrator
*
*/
public class BookDao extends BaseDao {
public int add(Book book) {
return (int) getHibernateTemplate().save(book);
}
public void edit(Book book) {
getHibernateTemplate().update(book);
}
public void del(Book book) {
getHibernateTemplate().delete(book);
}
public List list(){
return (List) getHibernateTemplate().execute(new HibernateCallback<List>() {
@Override
public List doInHibernate(Session session) throws HibernateException {
return session.createQuery(“from Book”).list();
}
});
}
}
(3、service层
BookBiz :
package com.lv.book.biz;
import java.util.List;
import com.lv.book.entity.Book;
public interface BookBiz {
public void add(Book book) ;
public List list() ;
}
BookBizImpl :
package com.lv.book.biz.Imp1;
import java.util.List;
import com.lv.book.biz.BookBiz;
import com.lv.book.dao.BookDao;
import com.lv.book.entity.Book;
public class BookBizImpl implements BookBiz{
private BookDao bookDao;
public BookDao getBookDao() {
return bookDao;
}
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
@Override
public void add(Book book) {
bookDao.add(book);
}
@Override
public List list() {
return bookDao.list();
}
}
**(4、action层:**BookAction
package com.lv.book.web;
import java.util.List;
import com.lv.base.web.BaseAction;
import com.lv.book.biz.BookBiz;
import com.lv.book.entity.Book;
public class BookAction extends BaseAction {
private Book book;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
private static final long serialVersionUID = 1L;
private BookBiz bookBiz;
public BookBiz getBookBiz() {
return bookBiz;
}
public void setBookBiz(BookBiz bookBiz) {
this.bookBiz = bookBiz;
}
public String add() {
// http://localhost:8080/test_SSH/sy/bookAction_add.action?book.bid=131418&&book.bname=zs&&book.price=1
this.bookBiz.add(book);
return “success”;
}
public String list() {
// http://localhost:8080/test_SSH/sy/bookAction_list.action
List list = this.bookBiz.list();
for (Book b : list) {
System.out.println(b);
}
return “success”;
}
}
(5、struts配置:struts-sy.xml
<?xml version="1.0" encoding="UTF-8"?>
“-//Apache Software Foundation//DTD Struts Configuration 2.5//EN”
“http://struts.apache.org/dtds/struts-2.5.dtd”>
/add.jsp
/index.jsp
(6、spring配置(spring-hibernate.xml,spring-模块.xml)
Spring-book.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:aop=“http://www.springframework.org/schema/aop”
xmlns:context=“http://www.springframework.org/schema/context” xmlns:tx=“http://www.springframework.org/schema/tx”
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
![img](https://i-blog.csdnimg.cn/blog_migrate/a2b5f5d9aedc9d6469c9b8a4739c7cdd.jpeg)
最后
无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。
针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
<?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:aop=“http://www.springframework.org/schema/aop”
xmlns:context=“http://www.springframework.org/schema/context” xmlns:tx=“http://www.springframework.org/schema/tx”
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。[外链图片转存中…(img-9vSFhv2p-1713706593486)]
[外链图片转存中…(img-9A0zkqFn-1713706593487)]
[外链图片转存中…(img-T6oaV1Gi-1713706593487)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
![img](https://i-blog.csdnimg.cn/blog_migrate/a2b5f5d9aedc9d6469c9b8a4739c7cdd.jpeg)
最后
无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。
针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。
[外链图片转存中…(img-eZfsx2kx-1713706593487)]
[外链图片转存中…(img-qmiNERER-1713706593488)]
[外链图片转存中…(img-2WH6Zn4J-1713706593488)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!