webservice (四) 整合spring mySQL等
创建一个Dynamic webProject,导包
文件结构
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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>crm_cxf</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cxf.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置CXF框架提供的servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 通过初始化参数指定CXF框架的配置文件位置 -->
<!-- <init-param>
<param-name>config-location</param-name>
<param-value>org.apache.cxf.transport.servlet.CXFServlet</param-value>
</init-param> -->
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
cxf.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:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///webservice"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 支持事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<context:component-scan base-package="com.wjl"></context:component-scan>
<bean id="customerService" class="com.wjl.service.CustomerService"/>
<!-- 注册服务 -->
<jaxws:server id="myService" address="/customer">
<jaxws:serviceBean>
<ref bean="customerService"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
创建实体类
package com.wjl.domain;
/**
* 客户实体类
* @author Administrator
*/
public class Customer {
private Integer id;
private String name;
private String station;
private String telephone;
private String address;
private String decidedzone_id;
public Customer()
{}
public Customer(Integer id, String name, String station, String telephone, String address, String decidedzone_id) {
super();
this.id = id;
this.name = name;
this.station = station;
this.telephone = telephone;
this.address = address;
this.decidedzone_id = decidedzone_id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStation() {
return station;
}
public void setStation(String station) {
this.station = station;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDecidedzone_id() {
return decidedzone_id;
}
public void setDecidedzone_id(String decidedzone_id) {
this.decidedzone_id = decidedzone_id;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", station=" + station + ", telephone=" + telephone
+ ", address=" + address + ", decidedzone_id=" + decidedzone_id + "]";
}
}
ICutomerService.java(注意:加注解)
package com.wjl.service;
import java.util.List;
import javax.jws.WebService;
import com.wjl.domain.Customer;
@WebService
public interface ICutomerService {
public List<Customer> findAll();
}
CustomerService.java
package com.wjl.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.annotation.Transactional;
import com.wjl.domain.Customer;
@Transactional
public class CustomerService implements ICutomerService{
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Customer> findAll() {
String sql="select *from t_customer";
List<Customer> list = jdbcTemplate.query(sql, new RowMapper<Customer>() {
public Customer mapRow(ResultSet rs, int arg1) throws SQLException {
int id = rs.getInt("id");//根据字段名称从结果集获取
String name = rs.getString("name");
String station = rs.getString("station");
String telephone = rs.getString("telephone");
String address = rs.getString("address");
String decidedzone_id = rs.getString("decidedzone_id");
return new Customer(id, name, station, telephone, address, decidedzone_id);
}
});
return list;
}
}