您的位置:首页 > 编程语言 > Java开发

spring+struts环境搭建

2017-09-08 15:32 337 查看
当pom.xml出现问题时,就是本地的仓库出现了问题(C:\Users\计算机名.m2\repository)删除出错的jar包

配置进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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: