Spring 整合Servlet

1 所需jar包

 

测试采用的是往数据库加信息

 

 实体类 :

package com.woniu.entity;

public class Users {
	private String uname;
	private double ubanance;
	public Users() {
		super();
	}
	
	public Users(String uname, double ubanance) {
		super();
		
		this.uname = uname;
		this.ubanance = ubanance;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public double getUbanance() {
		return ubanance;
	}
	public void setUbanance(double ubanance) {
		this.ubanance = ubanance;
	}

}

Dao接口

package com.woniu.Dao;

import org.apache.ibatis.annotations.Param;

import com.woniu.entity.Users;

public interface UserDao2 {
	void banance(@Param("userName") String uname,@Param("userBanance")double banance);
	void addusers( Users users);

}

实现类

package com.woniu.Dao;

import com.woniu.entity.Users;

public class UserDaoimp2 implements UserDao2{
	@Override
	public void banance(String uname, double banance) {
	}
	@Override
	public void addusers(Users users) {
	}

}

Dao的映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.woniu.Dao.UserDao2">
<update id="banance">
UPDATE users set ubanance=ubanance+#{userBanance} where uname=#{userName}
</update>
<insert id="addusers">
INSERT INTO users VALUES(null,#{uname},#{ubanance})
</insert>

</mapper>

IOC

package com.woniu.service;

import com.woniu.entity.Users;

public interface UserService2 {

	void adduser(Users user);	
}
package com.woniu.service;

import com.woniu.Dao.UserDao2;
import com.woniu.entity.Users;

public class UserServiceimp2 implements UserService2{

	UserDao2 userDao2;


	public void setUserDao2(UserDao2 userDao2) {
		this.userDao2 = userDao2;
	}

	@Override
	public void adduser(Users user) {
		userDao2.addusers(user);
	}
}

Servlet

package com.woniu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import com.woniu.entity.Users;
import com.woniu.service.UserService2;


@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	UserService2 userService2;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String uname = request.getParameter("uname");
		String ubanance = request.getParameter("ubanance");
		//创建user对象
		Users users=new Users(uname,Double.parseDouble(ubanance));
		/*
		 * 将创建的对象存放在Application中
		 * 实现容器只创建一个对象,不会重复创建,提高性能
		 * 需要在XML中配置contextConfigLocation,路径是Spring主配置文件的路径
		 * 
		 */
		ApplicationContext ac=(ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		userService2=(UserService2) ac.getBean("userservice2");
		userService2.adduser(users);
		//重定向
		response.sendRedirect("index.html");
	}

}

主配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- 这些所有可以在帮助文档中通过搜索  beans.xsd 复制而得 
使用注解 需要导入
xmlns:context="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd
-->
<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: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://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd ">
        
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <!-- mybatis主配置文件位置 -->
    <property name="configLocation" value="classpath:mybatisConfig.xml"></property>
    <!-- 数据源连接池 使用c3p0 -->
    <property name="dataSource" ref="dataSource"></property>
    </bean>    
    <!-- 配置JDBC -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
     <property name="driverClass" value="${jdbc.driver}" />
     <property name="jdbcUrl" value="${jdbc.url}" />
     <property name="user" value="${jdbc.userName}" />
     <property name="password" value="${jdbc.password}" />
     </bean>  
     <!-- 读取配置文件 --> 
     <context:property-placeholder location="classpath:JDBC.properties"/>
   <!-- 扫描指定包下的所有Dao接口,自动生成该接口的实现类 -->
     <bean id="mapperScannerConfigurer" 
     class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <property name="basePackage" value="com.woniu.Dao"/>
     <property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"/>
     </bean>
  
     <bean id="userservice2" class="com.woniu.service.UserServiceimp2">
     <property name="userDao2" ref="userDao2"/>
     </bean> 
     <!--没有事务的话可以不要    注册事务管理,Spring控制事务的核心类-->
     <bean id="transactionManager" 
     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"/>
     </bean>
    <!--没有事务的话可以不要   事务注解驱动,底层扫描注解事务注解,注入相关的事务操作代码 -->
     <tx:annotation-driven transaction-manager="transactionManager"/>
     
</beans> 

mybatis配置文件

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE configuration 
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
   "http://mybatis.org/dtd/mybatis-3-config.dtd">
   <configuration>
   <!-- 读取配置文件中的数据库信息 -->
   <properties  resource="JDBC.properties"/>
   <typeAliases>
 	<!-- <typeAlias type="Entity.Cgoods" alias="Cgoods"/> --><!-- 指定类名作为全限定名 -->
	<package name="com.woniu.entity"/> <!-- 指定包中所有类都使用类名代替全限定名 -->   
    </typeAliases>
   <!-- JDBC4大参数,数据源,连接池 全部交给Sping来做 -->
   
   <mappers>
   <!-- 管理com.woniu.Dao 下的所有映射文件 -->
   <package name="com.woniu.Dao"/>
   </mappers>
   </configuration>

JDBC配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///bank
jdbc.userName=root
jdbc.password=123456

web,xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Spring-web</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>
  <!-- 设置监听器 
  	配置监听器的全限定名-->
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- application的初始化参数 
       将创建的容器存放到Application中
       为了解决容器重复创建对象的问题
  -->
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <!-- classpath  :类路径下  解决找不到文件(让他去类路径下去找) -->
 <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
</web-app>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值