spring+struts环境搭建
2017-09-08 15:32
337 查看
当pom.xml出现问题时,就是本地的仓库出现了问题(C:\Users\计算机名.m2\repository)删除出错的jar包
配置进spring中需要给struts指定spring容器位置,在web.xml中写入监听
追加spring,jar包,spring-jdbc,aop,web,test进入maven仓库中
写一个实体类和要查询的表字段有一样的属性值对应
编写该表的dao接口,和实现类,注入jdbctemplate进行操作
dao接口实现类
配置JdbcNoteDao组件,application,spring.xml
测试类
配置进spring中需要给struts指定spring容器位置,在web.xml中写入监听
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
追加spring,jar包,spring-jdbc,aop,web,test进入maven仓库中
写一个实体类和要查询的表字段有一样的属性值对应
编写该表的dao接口,和实现类,注入jdbctemplate进行操作
dao接口实现类
@Repository("examDao") public class ExamPaPreDaoImpl implements ExamPaPreDao { @Resource private JdbcTemplate jdbcTemplate; @Override public List getAll() { return jdbcTemplate.query("select * from exam_paper", new ExamPaperMapper()); } @Override public ExamPaper getByPaperId(int paperId) { Object[] obj ={paperId}; return (ExamPaper) jdbcTemplate.query("select * from exam_paper where paper_id=? ",obj ,new ExamPaperMapper()); } }
*Dao返回值实体对象Mapper类*
public class ExamPaperMapper implements RowMapper<ExamPaper> { @Override public ExamPaper mapRow(ResultSet rs, int rowNum) throws SQLException { ExamPaper ep= new ExamPaper(); ep.setPaperId(rs.getInt("paper_id")); ep.setQuestionId(rs.getInt("question_id")); return ep; } }
配置JdbcNoteDao组件,application,spring.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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <!-- 扫描Action --> <context:component-scan base-package="cn.xdl.action"/> <!-- 扫描Dao --> <context:co a795 mponent-scan base-package="cn.xdl.dao"/> <!-- JdbcTemplate对象 --> <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="c3p0"></property> </bean> <!-- c3p0 --> <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="root"></property> <property name="password" value="1234"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/studyonline?useUnicode=true&characterEncoding=utf8"></property> </bean> </beans>
测试类
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class TestDao { @Resource private ExamPaperDao examDao; @Test public void test1(){ List list = examDao.getAll(); System.out.println(list); } }
相关文章推荐
- Eclipse下搭建struts、spring、hibernate开发环境
- JAVA-WEB的SSH(Spring Hibernate Struts)环境搭建一(Spring+Hibernate)
- Spring3-hibernate3.3-struts2.1.8----环境搭建(2)
- ssh环境搭建(2)-config篇(spring4.2.6+struts2.5+hibernate5.1 )
- 条理清晰的搭建SSH环境之整合Struts和Spring
- ibatis 开发中的经验 (三)Struts+Spring+Ibatis 开发环境搭建
- eclipse环境搭建ssh(Struts2+Spring2.5+Hibernate3)框架小谈(上)
- Maven搭建Spring+Struts2+Mybatis项目(一) Maven环境
- 条理清晰的搭建SSH环境之整合Struts和Spring
- ibatis 开发中的经验 (三)Struts+Spring+Ibatis 开发环境搭建
- spring4.06 hibernate4.31 struts2.3.16 全注解MAVEN环境搭建
- Spring,hibernate,struts1环境搭建步骤
- struts2+Hibernate4+spring3+EasyUI环境搭建之二:搭建spring
- struts2+Hibernate4+spring3+EasyUI环境搭建之四:引入hibernate4以及spring3与hibernate4整合
- struts2+Hibernate4+spring3+EasyUI环境搭建之三:引入sututs2以及spring与sututs2整合
- eclipse环境搭建ssh(Struts2+Spring2.5+Hibernate3)框架小谈(下)
- spring环境搭建以及和struts整合
- Spring+Struts2+mybatis的环境搭建
- 条理清晰的搭建SSH环境之整合Struts和Spring
- spring+struts+hibernate集成环境(基于maven项目搭建)