Spring + Struts +Mybatis 实现单表的增删改查

Spring + Struts +Mybatis 实现单表的增删改查

Bean

package com.ambow.bean;

public class DeptBean {
private int id;
private String dname;
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getDname() {
	return dname;
}
public void setDname(String dname) {
	this.dname = dname;
}
public DeptBean() {
	super();
}
public DeptBean(int id, String dname) {
	super();
	this.id = id;
	this.dname = dname;
}
@Override
public String toString() {
	return "DeptBean [id=" + id + ", dname=" + dname + "]";
}

}

Mapper层

package com.ambow.mapper;

import java.util.List;

import com.ambow.bean.DeptBean;


public interface DeptMapper {
/**
 * dept表CRUD
 * @param id
 * @return
 */
	//根据id查
	public DeptBean getById(int id);
	//查询所有
	public List<DeptBean> getAll();
    //添加
	public void deptAdd(DeptBean deptBean);
	//删除
	public void deptDel(int id);
	//修改
	public void deptUpdate(DeptBean deptBean);
	//模糊查询
	public List<DeptBean> getMoHu(String dname);
	
}

Mapper映射文件

<?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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 
	例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀) -->
<mapper namespace="com.ambow.mapper.DeptMapper">
	<!--根据id查 -->
	<select id="getById" parameterType="int"  resultType="DeptBean">
		select * from tb_dept where id=#{id}
	</select>
	<!-- 查所有 -->
	<select id="getAll" resultType="DeptBean">
	select * from tb_dept
	</select>
	<!-- 添加 -->
	<insert id="deptAdd" parameterType="com.ambow.bean.DeptBean">
		insert into tb_dept values(null,#{dname})
	</insert>
	<!-- 删除 -->
	<delete id="deptDel">
		delete from tb_dept where id=#{id}
	</delete>
	<!-- 修改 -->
	<update id="deptUpdate" parameterType="com.ambow.bean.DeptBean">
		update tb_dept set dname=#{dname} where id = #{id}
	</update>
	<!-- 模糊查询 -->
	<select id="getMoHu" parameterType="String" resultType="DeptBean">
	select * from tb_dept where dname like "%"#{dname}"%";
	</select>
</mapper>

Service层

package com.ambow.service;

import java.util.List;

import com.ambow.bean.DeptBean;
import com.ambow.mapper.DeptMapper;

public class DeptService {

	private DeptMapper deptMapper;
public void setDeptMapper(DeptMapper deptMapper) {
	this.deptMapper = deptMapper;
}

//查看所有
public List<DeptBean> getAllDept(){
	return deptMapper.getAll();
	
}
//根据id查
public DeptBean getByIdDept(int id){
	return deptMapper.getById(id);
	
}
//删除
public void delDept(int id){
	deptMapper.deptDel(id);
}
//修改
public void updateDept(DeptBean dept){
deptMapper.deptUpdate(dept);
}
//添加
public void addDept(DeptBean deptBean){
	deptMapper.deptAdd(deptBean);
} 
//模糊查询
public List<DeptBean> getMoHu(String dname){
	return deptMapper.getMoHu(dname);
}
}

Action层

package com.ambow.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;

import com.ambow.bean.DeptBean;
import com.ambow.service.DeptService;
import com.opensymphony.xwork2.ActionContext;

public class DeptAction implements RequestAware{
private DeptService deptService;
public void setDeptService(DeptService deptService) {
	this.deptService = deptService;
}
private DeptBean deptBean;
private int id;
private String dname;
public void setDname(String dname) {
	this.dname = dname;
}
public String getDname() {
	return dname;
}
public void setId(int id) {
	this.id = id;
}
public int getId() {
	return id;
}
public void setDeptBean(DeptBean deptBean) {
	this.deptBean = deptBean;
}
public DeptBean getDeptBean() {
	return deptBean;
}

//添加
public String addDept(){
	deptService.addDept(deptBean);
	return "ok";
	} 
//查看所有
public String allDept(){
Map<String,Object> map=(Map<String, Object>) ActionContext.getContext().get("request");
map.put("deptList", deptService.getAllDept());
return "deptList";
}
//修改
public String updateDept(){
	deptService.updateDept(deptBean);
	System.out.println("...修改"+deptBean.getId());
	System.out.println("..."+deptBean.getDname());
	return "ok";
	}
//删除
public String delDept(){
	deptService.delDept(id);
	return "ok";
}

public String byIdDept(){
DeptBean deptBean=deptService.getByIdDept(id);
System.out.println("...ById"+id);
//下面两行代码,是jsp 传值
Map<String,Object>	map=(Map<String,Object>) ActionContext.getContext().get("request");
map.put("deptById",deptBean);	
return "byId";
	}

//模糊查询
public String getMoHu(){
	List<DeptBean> deptBean=deptService.getMoHu(dname);
	System.out.println("...MuHu"+dname);
/*	Map<String,Object>	map=(Map<String,Object>) ActionContext.getContext().get("request");
	map.put("deptByDname",deptBean);*/
	request.put("deptByDname", deptBean);
	return "MuHu";
	}

Map<String, Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
this.request=arg0;
	
}

}

配置文件

Spring

<?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:aop="http://www.springframework.org/schema/aop"
	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-4.3.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
		 
	 <!-- 导入资源 -->
     <context:property-placeholder location="classpath:jdbc.properties"/>
     <!-- 配置C3P0 -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.uname}"></property>
        <property name="password" value="${jdbc.upwd}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="initialPoolSize" value="${jdbc.minpool}"></property>
        <property name="maxPoolSize" value="${jdbc.maxpool}"></property>
     </bean>
    
	<!-- 配置 sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	  <property name="dataSource" ref="dataSource"></property>
	 
	  <property name="configLocation" value="classpath:mybatis.xml"></property>
	   <!--  <property name="mapperLocations" value="classpath:com/ambow/entity/*.xml" ></property> -->
	   <!-- <property name="packagesToScan">
    	 <list>
                <value>com.ambow.entity</value>
         </list>
      </property> -->
	</bean>
	
	<!-- 配置事物 -->
	<!-- 配置mybatis的事物管理器 -->
	<bean id = "transactionManager" class=" org.springframework.jdbc.datasource.DataSourceTransactionManager">
	   <property name="dataSource" ref="dataSource"></property>
	</bean>
	<!--  开启事务注解
	<tx:annotation-driven transaction-manager="transactionManager"/> -->
	
	<!-- 配置事物属性 -->
	<tx:advice id ="txAdvice" transaction-manager="transactionManager">
	
	   <tx:attributes>
	       <tx:method name="get*" read-only="true"/>
	       <tx:method name="*"/>
	   </tx:attributes>
	</tx:advice>
	
	<!-- 配置事务切入点 -->
	<aop:config>
	    <aop:pointcut expression="execution (* com.ambow.service.*.*(..))" id="txPointCut"/>
	    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
	</aop:config>
  	<bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
    <property name="basePackage" value="com.ambow.mapper"></property>
    </bean>	
  	<bean id="deptService" class="com.ambow.service.DeptService">
  	<property name="deptMapper" ref="deptMapper"></property>
  	</bean>
<bean id="deptAction" class="com.ambow.action.DeptAction">
<property name="deptService" ref="deptService"></property>
</bean>
    

</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"></properties>
    <typeAliases>  <!-- 起别名 -->
        <!-- <typeAlias type="com.ambow.bean.DeptInfo" alias="DeptInfo"/>  这里是自定义别名
        <typeAlias type="com.ambow.bean.EmpInfo" alias="EmpInfo"/> -->
        <package name="com.ambow.bean"/>  <!-- 将指定包下的所有类引入,别名名称就是类名 -->
    </typeAliases>
    
    <!-- 环境变量的配置  default 与 envireonment 中的id 保持一致 -->
	<environments default="hehe">
		<environment id="hehe">
			<transactionManager type="JDBC" /> <!-- 事务管理  jdbc 事务提交及回滚   MANAGED :不做事务提交和回滚 -->
			<dataSource type="POOLED"><!-- UNPOOLED 不支持数据源连接池   POOLED 支持JDBC数据源连接  JNDI 支持外部数据源连接池 -->
				<property name="driver" value="${jdbc.driver}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.uname}" />
				<property name="password" value="${jdbc.upwd}" />
			</dataSource>
		</environment>
	</environments>
	
	
	<mappers>  <!--映射文件  -->
		<!-- <mapper resource="com/ambow/bean/DeptInfoMapper.xml" />
		<mapper resource="com/ambow/bean/EmpInfoMapper.xml" /> -->
		<package name="com.ambow.mapper"/>
	</mappers>
</configuration>

Struts

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

        <action name="dept_*" class="deptAction"  method="{1}">
            <result name="ok" type="redirectAction">dept_allDept</result>
            <result name="deptList">deptList.jsp</result>
            <result name="byId">deptUpdate.jsp</result>
            <result name="MuHu">MuHu.jsp</result>
        </action> 
        
       <!--   <action name="emp_*" class="empAction"  method="{1}">
            <result name="add" >empAdd.jsp</result>
            <result name="ok" type="redirectAction">emp_allEmp</result>
            <result name="byId">empUpdate.jsp</result>
            <result name="empList">empList.jsp</result>
        </action>  -->
       
       

    </package>



</struts>

web

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name></display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	   <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>
		
		<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
		
</web-app>


jar包+jsp
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191011100402453.PNG)
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019101110044138.PNG)





发布了14 篇原创文章 · 获赞 2 · 访问量 309

猜你喜欢

转载自blog.csdn.net/qq_43745587/article/details/102495468