JDBCtemplate 与struts2和spring 的整合(demo)

maven 项目直接上所需依赖:

        <!--servelt servlet的api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- struts2 的依赖 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.12</version>
        </dependency>
        <!-- struts2零配置插件 @Action (关于注解) -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.5.12</version>
        </dependency>

        <!-- spring context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>


        <!-- spring 对于web技术的一些支持 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>

        <!-- spring 对于orm技术的一些支持  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.5.12</version>
        </dependency>

        <!-- mysql 连接包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.43</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.41</version>
        </dependency>

配置spring :

<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"
    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/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd"
    default-autowire="byType">
    <!-- 扫描包 -->
    <context:component-scan base-package="com.woomoon"></context:component-scan>

    <!--配置数据源 jdbcTempLate  -->
    <bean id="jdbcTempLate" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="jdbc:mysql://127.0.0.1/test" />
        <property name="username" value="root" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="password" value="qwe" />
    </bean>

</beans>

建一个java 类注入template :

package com.woomoon.test;

import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import com.woomoon.entitys.StuEntity;

@Repository("testJdbc")
public class JDBCTempLateTest {

    public NamedParameterJdbcTemplate template;

    public List<StuEntity> queryAllList() {
        String sql = "select * from tb_stu";
        // 属性SQL参数源
        SqlParameterSource sps = new BeanPropertySqlParameterSource(new StuEntity());
        //行映射
        RowMapper<StuEntity> mapper = new BeanPropertyRowMapper<>(StuEntity.class);
        List<StuEntity> tempv = template.query(sql, sps, mapper);
        return tempv;
    }

    @Resource   //注入依赖
    @Qualifier(value = "jdbcTempLate") // 指定一个数据源(spring.xml 数据源bean标签中的id)
    public void setTemplate(DataSource dataSource) {
        this.template = new NamedParameterJdbcTemplate(dataSource);
    }
}

建一个测试了,测试template :

package com.woomoon.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    private static ClassPathXmlApplicationContext cp;

    public static void main(String[] args) {
        cp = new ClassPathXmlApplicationContext("spring.xml");  //初始化spring容器
        JDBCTempLateTest test = (JDBCTempLateTest) cp.getBean("testJdbc");
        test.queryAllList();
    }
}

返回结果:
return result

再来一个action :

package com.woomoon.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.woomoon.entitys.StuEntity;
import com.woomoon.test.JDBCTempLateTest;

public class TestAction {

    @Autowired
    JDBCTempLateTest test;

    @Action(value="index",results={@Result(name="success",location="/WEB-INF/html/index.html")})
    public String index() {
        return "success";
    }
    @Action(value="testQuery")
    public void queryTool () throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        //调用注入进来的类,将结果write到浏览器上
        List<StuEntity> list = test.queryAllList();

        response.getWriter().write(list.toString());
    }
}

到web.xml 配置一下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>stu_ssh</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>default.html</welcome-file>
    </welcome-file-list>
    <!-- struts  拦截所有的请求    *.action 也可以 -->
    <filter>
        <filter-name>stu_ssh</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>stu_ssh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--使用 ContextLoaderListener 初始化spring 容器-->
    <listener>
        <display-name>spring-web</display-name>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
</web-app>

访问testQuery.action 看访问结果:

return result

到这里jdbctemplate 的简单使用与struts2 和spring 的整合就写完了,一个demo ,一个过程,一个脚印,加油!
转载请注明出处,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值