Spring+SpringMVC+Mybatis+Maven+CXF+WebService整合之服务端
分类:
IT文章
•
2022-08-13 00:26:16
1 package com.javen.model;
2
3 import java.io.Serializable;
4
5 public class User implements Serializable{
6 private Integer id;
7
8 private String userName;
9
10 private String password;
11
12 private Integer age;
13
14 public Integer getId() {
15 return id;
16 }
17
18 public void setId(Integer id) {
19 this.id = id;
20 }
21
22 public String getUserName() {
23 return userName;
24 }
25
26 public void setUserName(String userName) {
27 this.userName = userName == null ? null : userName.trim();
28 }
29
30 public String getPassword() {
31 return password;
32 }
33
34 public void setPassword(String password) {
35 this.password = password == null ? null : password.trim();
36 }
37
38 public Integer getAge() {
39 return age;
40 }
41
42 public void setAge(Integer age) {
43 this.age = age;
44 }
45
46 @Override
47 public String toString() {
48 return "User [
49 + password + ", age=" + age + "]";
50 }
51 }
User
1 package com.javen.dao;
2
3 import java.util.ArrayList;
4 import java.util.Map;
5
6 import com.javen.model.User;
7
8 public interface UserMapper{
9
10 public ArrayList<User> selectAll();
11
12 public Integer selectLogin(Map map);
13
14 public User selectById(int id);
15
16 public void updateById(int id);
17
18 public void deleteById(int id);
19 }
UserMapper
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 <mapper namespace="com.javen.dao.UserMapper">
4 <resultMap id="BaseResultMap" type="com.javen.model.User">
5 <id column="id" jdbcType="BIGINT" property="id" />
6 <!-- column 代表数据库一方,property代表实体类一方 -->
7 <result column="user_name" jdbcType="VARCHAR" property="userName" />
8 <result column="password" jdbcType="VARCHAR" property="password" />
9 <result column="age" jdbcType="BIGINT" property="age"/>
10 </resultMap>
11 <!-- resultMap返回复数的结果 -->
12 <select id="selectAll" resultMap="BaseResultMap">
13 select * from user_t
14 </select>
15 <!-- 参数类型为Map,可以有多个值,返回值类型为Integer -->
16 <select id="selectLogin" parameterType="Map" resultType="Integer">
17 select id from user_t where user_name=#{userName} and password=#{password}
18 </select>
19
20 </mapper>
UserMapper.xml
1 package com.javen.service;
2 import java.util.ArrayList;
3 import java.util.Map;
4 import javax.jws.WebMethod;
5 import javax.jws.WebService;
6 import com.javen.model.User;
7 //表示这是一个@WebService服务接口
8 @WebService
9 public interface IUserService {
10 //@WebMethod注解,表示服务发布时被注解的方法也会随之发布
11 @WebMethod
12 public ArrayList<User> selectAll();
13 @WebMethod
14 public Integer selectLogin(Map map);
15 @WebMethod
16 public User selectById(int id);
17 @WebMethod
18 public void updateById(int id);
19 @WebMethod
20 public void deleteById(int id);
21
22 }
IUserService
1 package com.javen.service.impl;
2
3 import java.util.ArrayList;
4 import java.util.Map;
5
6 import javax.jws.WebService;
7
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.stereotype.Service;
10
11 import com.javen.dao.UserMapper;
12 import com.javen.model.User;
13 import com.javen.service.IUserService;
14 //这是接口的实现类。 endpointInterface属性,格式:包名.接口名
15 @Service
16 @WebService(endpointInterface="com.javen.service.IUserService")
17 public class UserServiceImpl implements IUserService{
18
19 @Autowired
20 UserMapper userDao;
21 // @Autowired
22 User user;
23
24
25 public void deleteById(int id) {
26 // TODO Auto-generated method stub
27
28 }
29
30 public ArrayList<User> selectAll() {
31 ArrayList<User> user=userDao.selectAll();
32 return user;
33
34 }
35
36 public Integer selectLogin(Map map) {
37 return userDao.selectLogin(map);
38 }
39
40 public User selectById(int id) {
41 // TODO Auto-generated method stub
42 return null;
43 }
44
45 public void updateById(int id) {
46 // TODO Auto-generated method stub
47
48 }
49
50
51
52 }
UserServiceImpl
1 package com.javen.controller;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import javax.servlet.http.HttpServletRequest;
8
9 //import org.slf4j.Logger;
10 //import org.slf4j.LoggerFactory;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.http.HttpRequest;
13 import org.springframework.stereotype.Controller;
14 import org.springframework.ui.Model;
15 import org.springframework.web.bind.annotation.PathVariable;
16 import org.springframework.web.bind.annotation.RequestMapping;
17 import org.springframework.web.bind.annotation.RequestMethod;
18 import org.springframework.web.bind.annotation.ResponseBody;
19
20 import com.javen.model.User;
21 import com.javen.service.IUserService;
22
23
24 @Controller
25 @RequestMapping("/")
26 public class UserController {
27 @Autowired
28 IUserService iUserService;
29
30 @RequestMapping("/")
31 public String login(){
32 return "login";
33 }
34
35 @RequestMapping(value="loginUser",method={RequestMethod.POST})
36 @ResponseBody
37 public int loginUser(HttpServletRequest request,Model model){
38 System.out.println("login...");
39 Map map=new HashMap();
40 String userName=request.getParameter("userName");
41 String password=request.getParameter("password");
42 System.out.println("账号:"+userName+"
"+"密码:"+password);
43 map.put("userName", userName);
44 map.put("password", password);
45 Integer id=iUserService.selectLogin(map);
46 System.out.println("id:"+id);
47 return id;
48
49 }
50
51 @RequestMapping("/test")
52 public String test(HttpServletRequest request,Model model){
53 System.out.println("test");
54 ArrayList<User> user=iUserService.selectAll();
55 model.addAttribute("user", user);
56 return "index";
57 }
58 }
UserController
1 package com.javen.listener;
2 //import java.net.MalformedURLException;
3 //import javax.xml.ws.Endpoint;
4 //import com.javen.service.impl.UserServiceImpl;
5
6 public class WebServicePublishListener{
7 //WebService服务发布
8 // public static void main(String[] args)
9 // throws MalformedURLException {
10 // Endpoint.publish("http://localhost:11111/WS_WebService", new UserServiceImpl());
11 //
12 // }
13 }
WebServicePublishListener
1 driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
2 url=jdbc:sqlserver://localhost:1433;databaseName=HydSSmDemo_TEST
3 username=ZOOUSER
4 password=ZOOUSER
jdbc.properties
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context-3.1.xsd
10 http://www.springframework.org/schema/mvc
11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12 <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
13 <context:component-scan base-package="com.javen.controller" />
14 <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
15 <mvc:annotation-driven/>
16 <!-- 静态资源处理 css js imgs -->
17 <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
18 <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
19 <!-- 启动SpringMVC的注解功能 -->
20 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
21 <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
22 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
23 <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
24 <property name="prefix" value="/WEB-INF/jsp/" />
25 <property name="suffix" value=".jsp" />
26 </bean>
27 </beans>
spring-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context-3.1.xsd
10 http://www.springframework.org/schema/mvc
11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12 <!-- 自动扫描 -->
13 <context:component-scan base-package="com.javen" />
14
15 <!-- 引入配置文件 -->
16 <bean id="propertyConfigurer"
17 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
18 <property name="location" value="classpath:jdbc.properties" />
19 </bean>
20
21 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
22 destroy-method="close">
23 <property name="driverClassName" value="${driver}" />
24 <property name="url" value="${url}" />
25 <property name="username" value="${username}" />
26 <property name="password" value="${password}" />
27 <!-- 初始化连接大小 -->
28 <property name="initialSize" value="${initialSize}"></property>
29 <!-- 连接池最大数量 -->
30 <property name="maxActive" value="${maxActive}"></property>
31 <!-- 连接池最大空闲 -->
32 <property name="maxIdle" value="${maxIdle}"></property>
33 <!-- 连接池最小空闲 -->
34 <property name="minIdle" value="${minIdle}"></property>
35 <!-- 获取连接最大等待时间 -->
36 <property name="maxWait" value="${maxWait}"></property>
37 </bean>
38
39 <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
40 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
41 <property name="dataSource" ref="dataSource" />
42 <!-- 自动扫描mapper.xml文件 -->
43 <property name="mapperLocations" value="classpath:com/javen/dao/*.xml"></property>
44 </bean>
45
46 <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
47 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
48 <property name="basePackage" value="com.javen.dao" />
49 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
50 </bean>
51
52 <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
53 <bean id="transactionManager"
54 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
55 <property name="dataSource" ref="dataSource" />
56 </bean>
57
58 </beans>
spring-mybatis.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:jaxws="http://cxf.apache.org/jaxws"
5 xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
7 default-lazy-init="true">
8 <!-- 引入CXF Bean定义如下,早期的版本中使用 。 -->
9 <!-- 3.0以下都需要引入这三个文件,3.0以上只需要cxf.xml这一个xml文件就好 -->
10 <!-- <import resource="classpath*:META-INF/cxf/cxf.xml" /> -->
11 <!-- <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" /> -->
12 <!-- <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />-->
13
14 <!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 发布webservice xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
15 <!-- WebService的实现Bean定义 -->
16 <!-- com.javen.service.impl.UserServiceImpl是接口实现类
17 web.xml配置了webservice的访问路径/server/*,那么/server/web-publish?wsdl就是该webservice的访问路径;
18 该webservice的访问路径前面要加上项目的路径 localhost:8080/项目名/server/web-publish?wsdl
19 -->
20 <bean id="iUserService" class="com.javen.service.impl.UserServiceImpl" />
21 <!-- 1.jax-ws endpoint定义
22 2.implementor="#iUserService",iUserService是接口,首字母小写
23 3. jaxws:endpoint标签的id自定义就好
24 4.address="/web-publish"是服务发布的地址,和web.xml中的<url-pattern>/server/*</url-pattern>标签的值相结合-->
25 <jaxws:endpoint id="myService" implementor="#iUserService"
26 address="/web-publish">
27 </jaxws:endpoint>
28 <!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 发布webservice xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
29 </beans>
spring-webservice.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
3 <display-name>HydSSmDemo</display-name>
4 <welcome-file-list>
5 <welcome-file>index.html</welcome-file>
6 <welcome-file>index.htm</welcome-file>
7 <welcome-file>index.jsp</welcome-file>
8 <welcome-file>default.html</welcome-file>
9 <welcome-file>default.htm</welcome-file>
10 <welcome-file>default.jsp</welcome-file>
11 </welcome-file-list>
12
13
14 <!-- Spring和mybatis和webservice的配置文件 -->
15 <context-param>
16 <param-name>contextConfigLocation</param-name>
17 <param-value>classpath:spring*.xml</param-value>
18 </context-param>
19 <!-- 编码过滤器 -->
20 <filter>
21 <filter-name>encodingFilter</filter-name>
22 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
23 <async-supported>true</async-supported>
24 <init-param>
25 <param-name>encoding</param-name>
26 <param-value>UTF-8</param-value>
27 </init-param>
28 </filter>
29 <filter-mapping>
30 <filter-name>encodingFilter</filter-name>
31 <url-pattern>/*</url-pattern>
32 </filter-mapping>
33 <!-- Spring监听器 -->
34 <listener>
35 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
36 </listener>
37 <!-- 防止Spring内存溢出监听器 -->
38 <listener>
39 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
40 </listener>
41
42 <!-- Spring MVC servlet -->
43 <servlet>
44 <servlet-name>SpringMVC</servlet-name>
45 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
46 <init-param>
47 <param-name>contextConfigLocation</param-name>
48 <param-value>classpath:spring-mvc.xml</param-value>
49 </init-param>
50 </servlet>
51 <servlet-mapping>
52 <servlet-name>SpringMVC</servlet-name>
53 <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
54 <url-pattern>/</url-pattern>
55 </servlet-mapping>
56
57 <!-- ??CXF?????Servlet -->
58 <servlet>
59 <servlet-name>CXFServlet</servlet-name>
60 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
61 <load-on-startup>2</load-on-startup>
62 </servlet>
63 <!--<url-pattern>/server/*</url-pattern> 服务发布的url,和address="/web-publish"还有项目路径相互组合成完整的服务发布链接
64 http://localhost:8080/WS_WebService/server/web-publish?wsdl -->
65 <servlet-mapping>
66 <servlet-name>CXFServlet</servlet-name>
67 <url-pattern>/server/*</url-pattern>
68 </servlet-mapping>
69
70 </web-app>
web.xml
1 $(function(){
2 $("#sub").click(function(){
3 var userName=$("#userName").val();
4 var password=$("#password").val();
5 $.ajax({
6 type:'post',
7 dataType:'json',
8 url:'./loginUser',
9 async: true,
10 data:{
11 userName:$("#userName").val(),
12 password:$("#password").val()
13 },
14 success:function(data){
15 if(data!=null){
16 alert("success");
17 window.location.href='./test';
18 }
19 }
20 });
21 });
22
23
24 $("#res").click(function(){
25 $("#userName").val("");
26 $("#password").val("");
27 });
28
29 });
table_Style.js
1 table tr td{
2 text-align:center;
3 }
table_Style.css
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
4
5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
6 <html>
7 <head>
8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
9 <title>Insert title here</title>
10 <link href="${pageContext.request.contextPath}/css/table_Style.css" rel="stylesheet" type="text/css">
11 <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js"></script>
12 <script type="text/javascript" src="${pageContext.request.contextPath}/js/table_Style.js"></script>
13 </head>
14 <body>
15 <h1 style='color:red'>Hello word!</h1>
16 <body>
17 <table id="" cellpadding="0" cellspacing="0" border="1" width="700">
18 <tr>
19 <th>序号</th><th>姓名</th><th>年龄</th>
20 </tr>
21 <c:forEach items="${user}" var="u" varStatus="stat">
22 <tr>
23 <td>${stat.index+1}</td><td>${u.userName}</td><td>${u.age}</td>
24 </tr>
25 </c:forEach>
26 </table>
27 </body>
28 </html>
index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7 <title>Insert title here</title>
8 <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js"></script>
9 <script type="text/javascript" src="${pageContext.request.contextPath}/js/table_Style.js"></script>
10 </head>
11 <body>
12 <h1>Login</h1>
13 <table id="" cellpadding="0" cellspacing="0" border="1" width="300">
14 <tr>
15 <td align="right">登录名:</td><td>
16 <input type="text" id="userName" name="userName">
17 </td>
18 </tr>
19 <tr>
20 <td align="right">密码:</td><td>
21 <input type="password" id="password" name="password">
22 </td>
23 </tr>
24 <tr>
25 <td align="center" colspan="2">
26 <input type="button" id="sub" value="登录">
27 <input type="reset" id="res" value="清空">
28 </td>
29 </tr>
30 </table>
31
32
33 </body>
34 </html>
login.jsp
项目启动成功后进入 http://localhost:8080/WS_WebService/server/web-publish?wsdl 页面查看 WSDL(WebService描述语言)
项目登录页面链接: http://localhost:8080/WS_WebService/