链接:https://pan.baidu.com/s/135Uda58Fm3RJibSWQYHiKQ 提取码:c46g
1、applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 加载数据库连接信息配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!--druid连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${druid.driver}"/> <property name="url" value="${druid.url}"/> <property name="username" value="${druid.username}"/> <property name="password" value="${druid.password}"/> </bean> <context:component-scan base-package="xuan.dao.*,xuan.service.*"/> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <!--配置Hibernate的方言--> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <!--格式化输出sql语句--> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.use_sql_comments">false</prop> </props> </property> <!-- 自动扫描实体 --> <property name="packagesToScan" value="xuan.entity" /> </bean> <!-- 配置 HibernateTemplate 对象 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <!-- 注入 SessionFactory 对象 --> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 用注解来实现事务管理 --> <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>2、springmvc.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:mvc="http://www.springframework.org/schema/mvc" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- SpringMVC使用<mvc:annotation-driven>自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter --> <mvc:annotation-driven /> <!-- 启动注解 --> <!-- 配置扫描注解 @Controller --> <context:component-scan base-package="xuan.controller"/> <!-- 配置静态资源映射 --> <!-- <mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/imgs/" mapping="/imgs/**"/> <mvc:resources location="/font/" mapping="/font/**"/> --> <!-- 配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置逻辑视图的前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 配置逻辑视图的后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>3、db.properties
druid.driver=com.mysql.jdbc.Driver druid.url=jdbc:mysql://localhost:3306/xuan?characterEncoding=utf-8 druid.username=root druid.password=root
1、TestController
package xuan.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import xuan.entity.TestEntity; import xuan.service.TestService; import xuan.utils.Singleton; import javax.servlet.http.HttpServletRequest; import java.util.List; /** * 控制层 * @author Xuan * @date 2019/9/17 17:53 */ @Controller public class TestController { private TestService testService = (TestService) Singleton.GetApplicationContext().getBean("TestServiceImpl"); /** * 获取所有数据并且返回到index.jsp页面 * @param request * @return */ @RequestMapping("/GetAll") public String test(HttpServletRequest request){ System.out.println("来过"); List<TestEntity> list = testService.GetAll(); for (TestEntity arr:list) { System.out.println(arr.getId()); } request.getSession().setAttribute("xuanlist",list); return "show"; } }2、TestDaoImpl
package xuan.dao.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.stereotype.Repository; import xuan.dao.TestDao; import xuan.entity.TestEntity; import javax.annotation.Resource; import java.util.List; /** * @author Xuan * @date 2019/10/2 3:23 */ @Repository public class TestDaoImpl implements TestDao { //提供Hibernate模板 @Autowired @Resource private HibernateTemplate hibernateTemplate; public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } @Override public List<TestEntity> GetAll() { return this.hibernateTemplate.loadAll(TestEntity.class); } }3、TestDao
package xuan.dao; import xuan.entity.TestEntity; import java.util.List; /** * 映射类接口 * @author Xuan * @date 2019/9/17 17:51 */ public interface TestDao { /** * 查询所有列表 * @return */ List<TestEntity> GetAll(); }4、TestEntity
package xuan.entity; import javax.persistence.*; /** * @author Xuan * @date 2019/10/2 4:35 */ @Entity @Table(name = "test", schema = "hibernate") public class TestEntity { private int id; private String name; @Id @Column(name = "id") public int getId() { return id; } public void setId(int id) { this.id = id; } @Basic @Column(name = "name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TestEntity that = (TestEntity) o; if (id != that.id) return false; if (name != null ? !name.equals(that.name) : that.name != null) return false; return true; } @Override public int hashCode() { int result = id; result = 31 * result + (name != null ? name.hashCode() : 0); return result; } }5、TestServiceImpl
package xuan.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import xuan.dao.TestDao; import xuan.entity.TestEntity; import xuan.service.TestService; import javax.annotation.Resource; import java.util.List; /** * Service服务层 * @author Xuan * @date 2019/9/17 17:54 */ @Service("TestServiceImpl") public class TestServiceImpl implements TestService { @Autowired @Resource private TestDao testmapper; public TestDao getTestmapper() { return testmapper; } public void setTestmapper(TestDao testmapper) { this.testmapper = testmapper; } @Override public List<TestEntity> GetAll() { return testmapper.GetAll(); } }6、TestService
package xuan.service; import xuan.entity.TestEntity; import java.util.List; /** * @author Xuan * @date 2019/9/17 17:53 */ public interface TestService { /** * 查询所有列表 * @return */ List<TestEntity> GetAll(); }7、Singleton
package xuan.utils; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 单列双检索模式 * @author Xuan * */ public class Singleton { /** * 进行访问控制 */ private Singleton(){ } private volatile static ApplicationContext applicationcontext; /** * 得到ApplicationContext对象的方法 * @return */ public static ApplicationContext GetApplicationContext(){ if (applicationcontext==null) { synchronized (Singleton.class) { if (applicationcontext==null) { applicationcontext = new ClassPathXmlApplicationContext("applicationContext.xml"); } } } return applicationcontext; } }8、show.jsp
<%@ page import="java.util.List" %> <%@ page import="xuan.entity.TestEntity" %><%-- Created by IntelliJ IDEA. User: Xuan Date: 2019/9/17 Time: 19:07 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Title</title> </head> <body> <% List<TestEntity> list = (List<TestEntity>) request.getSession().getAttribute("xuanlist"); for (TestEntity arr :list) { response.getWriter().println("ID是"+arr.getId()+"姓名是:"+arr.getName()); } %> <%-- <c:forEach var="r" items="${xuanlist}" varStatus="vs">--%> <%-- ID是:${r}--%> <%-- 姓名是:${r.name}--%> <%-- </c:forEach>--%> </body> </html>
1、我的数据库数据 2、访问效果
