zoukankan      html  css  js  c++  java
  • Mybatis+struts2+spring整合

    把student项目改造成ssm  struts2 +mybatis+spring

    1,先添加spring支持:类库三个,applicationContext.xml写在webinf下四个命名空间,监听器

    2,添加struts2支持  struts2与spring整合的jar包

    3,添加mybatis2支持,把jar包导入,mybatis与spring整合的jar包,把原来在mybatis.cfg.xml中的大部分配置都写在applicationContext.xml,跟hibernate一样,只不过像日志输出方式与分页插件还是需要写在mybatis.cfg.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:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:p="http://www.springframework.org/schema/p"
    	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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    	<context:component-scan base-package="cn.bdqn.student"/>
    	
    	<!-- 配置数据源 -->
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    	p:driverClassName="oracle.jdbc.OracleDriver"
    		p:url="jdbc:oracle:thin:@localhost:1521:ORCL"
    		p:username="stuinfo"
    		p:password="ok"
    	/>
    	<!-- 配置SQLSessionFactory "dataSource"注入数据源 typeAliasesPackage把这个实体类下的所有都自动设置别名-->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
    		p:dataSource-ref="dataSource"
    		p:mapperLocations="classpath:cn/bdqn/student/mapper/*.xml"
    		p:typeAliasesPackage="cn.bdqn.student.entity"
    		p:configLocation="classpath:mybatis.cfg.xml"
    	/>
    	
    	<!-- 配置事务管理器 -->
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    		p:dataSource-ref="dataSource"
    	/>
    	
    	<!-- 事务增强 -->
    	<tx:advice id="txAdvice">
    		<tx:attributes>
    			<tx:method name="*"/>
    		</tx:attributes>
    	</tx:advice>
    	
    	<!-- 事务切面 -->
    	<aop:config>
    		<aop:pointcut expression="execution(* cn.bdqn.student.service..*.*(..))" id="txMethods"/>
    		<aop:advisor advice-ref="txAdvice" pointcut-ref="txMethods"/>
    	</aop:config>
    	
    	<!-- 配置动态创建Mapper实现类对象 -->	
    	<!-- 
    	<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    		p:mapperInterface="cn.bdqn.student.mapper.UserMapper"
    		p:sqlSessionFactory-ref="sqlSessionFactory"
    	/>  
            
       	<bean id="courseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    		p:mapperInterface="cn.bdqn.student.mapper.CourseMapper"
    		p:sqlSessionFactory-ref="sqlSessionFactory"
    	/>  
    	
    	<bean id="educationMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    		p:mapperInterface="cn.bdqn.student.mapper.EducationMapper"
    		p:sqlSessionFactory-ref="sqlSessionFactory"
    	/>
    	
    	<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
    		p:mapperInterface="cn.bdqn.student.mapper.StudentMapper"
    		p:sqlSessionFactory-ref="sqlSessionFactory"
    	/>
    	-->
    	
    	<!-- 扫描指定的包,根据映射自动生成Mapper实现类对象 -->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
    		p:basePackage="cn.bdqn.student.mapper"
    	/>  
    
    </beans>
    
    <?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="cn.bdqn.student.mapper.StudentMapper">
    	<insert id="saveStudent" parameterType="Student">
    		INSERT INTO Student(id,name,sex,birthday,cid,eid,contact,time,state,addTime,pic) VALUES(
    		SEQ_STUDENT.nextval,
    		#{name},
    		#{sex},
    		#{birthday},
    		#{course.id},
    		#{education.id},
    		#{contact},
    		#{time},
    		#{state},
    		#{addtime},
    		#{pic}
    		)
    	</insert>
    	
    	<select id="getStudent" resultMap="studentMap">
    		SELECT 
          	  s.id,
    		  s.name,
    		  s.sex,
    		  s.birthday,
    		  s.cid,
    		  s.eid,
    		  s.contact,
    		  s.time,
    		  s.state,
    		  s.addTime,
    		  c.name AS cname,
    		  e.name AS ename,
          	  s.pic
    		FROM Student s INNER JOIN Course c ON s.cid=c.id
    		               INNER JOIN Education e ON s.eid=e.id
    		WHERE s.id=#{id}
    		
    	</select>
    	
    	<select id="findStudent" resultMap="studentMap">
    		SELECT 
          	  s.id,
    		  s.name,
    		  s.sex,
    		  s.birthday,
    		  s.cid,
    		  s.eid,
    		  s.contact,
    		  s.time,
    		  s.state,
    		  s.addTime,
    		  c.name AS cname,
    		  e.name AS ename,
          	  s.pic
    		FROM Student s INNER JOIN Course c ON s.cid=c.id
    		               INNER JOIN Education e ON s.eid=e.id
    		<where>
    			<if test="name!=null">s.name LIKE #{name}</if>
    			<if test="courseId">AND s.cid=#{courseId}</if>
    			<if test="time!=null">AND time=#{time}</if>
    			<if test="educationId!=null">AND s.eid=#{educationId}</if>
    			<if test="state!=null">AND state=#{state}</if>
    		</where> 	
    		ORDER  BY s.id DESC
    	</select>
    	
    	<resultMap id="studentMap" type="Student">
    		<id column="id" property="id"/>
    		<result column="name" property="name"/>
    		<result column="sex" property="sex"/>
    		<result column="birthday" property="birthday"/>
    		<result column="contact" property="contact"/>
    		<result column="time" property="time"/>
    		<result column="state" property="state"/>
    		<result column="addTime" property="addtime"/>
    		<result column="birthday" property="birthday"/>
    		
    		<association property="course" javaType="Course">
    			<id column="cid" property="id"/>
    			<result column="cname" property="name"/>
    		</association>
    		
    		<association property="education" javaType="Education">
    			<id column="eid" property="id"/>
    			<result column="ename" property="name"/>
    		</association>
    		
    	</resultMap>
    	
    	<update id="updateStudent" parameterType="Studnet">
    		UPDATE Student SET
    			name=#{name},
    			sex=#{sex},
    			birthday=#{birthday},
    			cid=#{course.id},
    			eid=#{education.id},
    			contact=#{contact},
    			time=#{time},
    			state=#{state},
    			pic=#{pic}
    		WHERE 
    			id=#{id}
    	
    	</update>
    	
    	<delete id="deleteStudent">
    		DELETE FROM Student WHERE id=#{id} 
    	
    	</delete>
    	
    	 
    	
    	
    	
    </mapper>
    
    package cn.bdqn.student.mapper;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Param;
    
    import cn.bdqn.mybatis.plugin.PageParam;
    import cn.bdqn.student.entity.Course;
    import cn.bdqn.student.entity.Student;
    
    public interface StudentMapper {
    	public List<Student> findStudent(
    			@Param("pageParam") PageParam param,
    			@Param("name") String name,
    			@Param("courseId") Integer courseId,
    			@Param("time") Integer time,
    			@Param("educationId") Integer educationId,
    			@Param("state") Integer state
    			);
    	
    	public void saveStudent(Student student); 
    	
    	public Student getStudent(Integer id);
    	
    	//更新学生
    	public void updateStudent(Student student);
    	
    	//删除学生
    	public void deleteStudent(Integer id);
    }
    
    package cn.bdqn.student.dao.student;
    
    import java.util.List;
    
    import org.apache.commons.lang.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    
    import cn.bdqn.mybatis.plugin.PageParam;
    import cn.bdqn.student.entity.Student;
    import cn.bdqn.student.mapper.StudentMapper;
    import cn.bdqn.student.util.PageBean;
    @Repository("studentDAO")
    public class StudentDAOImpl implements IStudentDAO{
    	
    	private StudentMapper studentMapper;
    	@Autowired
    	public void setStudentMapper(StudentMapper studentMapper) {
    		this.studentMapper = studentMapper;
    	}
    
    	@Override
    	public void saveStudent(Student student) {
    		studentMapper.saveStudent(student);
    	}
    
    	@Override
    	public PageBean findStudent(int pageIndex, int pageSize, String name,
    			Integer courseId, Integer time, Integer educationId, Integer state) {
    		PageBean p=new PageBean();
    		PageParam param=new PageParam();
    		
    		if(StringUtils.isEmpty(name) ){
    			name=null;
    		}//由于name是“”空字符串,但我在判断的时候却是以空来进行判断的,所以需要进行处理
    		
    		List<Student> results=studentMapper.findStudent(param, name, courseId, time, educationId, state);
    		p.setPageIndex(pageIndex);
    		p.setPageSize(pageSize);
    		p.setPageCount(param.getPageCount());
    		p.setResults(results);
    		p.setRowCount(param.getRowCount());
    		
    		return p;
    	}
    
    	@Override
    	public Student getStudent(Integer id) {
    		// TODO Auto-generated method stub
    		return studentMapper.getStudent(id);
    	}
    
    	@Override
    	public void updateStudent(Student student) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void deleteStudent(Integer id) {
    		// TODO Auto-generated method stub
    		
    	}
    
    }
    
  • 相关阅读:
    SQL0668N 由于表 "db2inst1.test" 上的原因代码 "3",所以不允许操作(解因为LOAD引起的LOAD暂挂状态锁)
    重装系统后,如何恢复DB2数据库?
    db2 导入导出命令大全
    解决tomcat启动时日志出现 javax.naming.NamingException: Invalid byte 1 of 1byte UTF8 sequence.的问题
    【fcntl系统调用】
    C++读书笔记之函数模板
    unix shell笔记
    用setsockopt()来控制recv()与send()的超时 【转】
    Linux守护进程的编程实现 [转]
    GDB调试core文件样例(如何定位Segment fault) 【转】
  • 原文地址:https://www.cnblogs.com/Angelinas/p/5633249.html
Copyright © 2011-2022 走看看