把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 } }

Mybatis+struts2+spring整合的更多相关文章

  1. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  2. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  3. mybatis与spring整合(基于配置文件)

    本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池. 1.编写数据访问接口(UserDa ...

  4. mybatis与spring整合时读取properties问题的解决

    在学习mybatis与spring整合是,想从外部引用一个db.properties数据库配置文件,在配置文件中使用占位符进行引用,如下: <context:property-placehold ...

  5. Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6964162.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(八)--My ...

  6. Mybatis第五篇【Mybatis与Spring整合】

    Mybatis与Spring整合 既然我们已经学了Mybatis的基本开发了,接下来就是Mybatis与Spring的整合了! 以下使用的是Oracle数据库来进行测试 导入jar包 aopallia ...

  7. MyBatis 与 Spring 整合

    MyBatis-Spring 项目 目前大部分的 Java 互联网项目,都是用 Spring MVC + Spring + MyBatis 搭建平台的. 使用 Spring IoC 可以有效的管理各类 ...

  8. Mybatis(六) Spring整合mybatis

    心莫浮躁~踏踏实实走,一步一个脚印,就算不学习,玩,能干嘛呢?人生就是那样,要找点有意思,打发时间的事情来做,而钻研技术,动脑动手的过程,还是比其他工作更有意思些~ so,努力啥的都是强迫自己做自以为 ...

  9. MyBatis学习(三)---MyBatis和Spring整合

    想要了解MyBatis基础的朋友可以通过传送门: MyBatis学习(一)---配置文件,Mapper接口和动态SQL http://www.cnblogs.com/ghq120/p/8322302. ...

随机推荐

  1. Sql Server Analysis Service 转换为UnknownMember的正确设置

    在SSAS中事实表数据被归类到为UnknownMember 的时候分为两种情况: 第一种情况,在SSAS里面事实表中的外键是null,这种情况SSAS在建事实表和维度时ErrorConfigurati ...

  2. 161101、在Java中如何高效判断数组中是否包含某个元素

    如何检查一个数组(无序)是否包含一个特定的值?这是一个在Java中经常用到的并且非常有用的操作.同时,这个问题在Stack Overflow中也是一个非常热门的问题.在投票比较高的几个答案中给出了几种 ...

  3. myeclipse启动报错 no java virtual machine。。。

    如果环境变量里已经配置了JAVA_HOME,但是在启动的时候还会提示下面的信息:   A Java Runtime Environment (JRE) or Java Development Kit ...

  4. c# 定时执行python脚本

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. IE11下,IIS7.5不自动生成__doPostBack事件

    MS在 2013/10/8 已出补丁 http://www.microsoft.com/zh-cn/download/details.aspx?id=39257 https://support.mic ...

  6. How To Set Up vsftpd on CentOS 6

    About vsftpd 警告:FTP是天生不安全.如果你必须使用FTP,考虑securing your FTP connection with SSL/TLS.否则,最好use SFTP, a se ...

  7. How To Create a SSL Certificate on Apache for CentOS 6

    About Self-Signed Certificates 自签证书.一个SSL证书,是加密网站的信息,并创建更安全的链接的一种方式.附加地,证书可以给网站浏览者显示VPS的的身份证明信息.如果一个 ...

  8. 微信开放平台API开发资料

    微信大概两年前开启了微信公众平台的API供开发者使用,从账号登陆.消息发送.用户账号管理.公众号菜单.客服接口.微信商店接口.用户卡券接口 以及微信支付接口.可以说是全方面覆盖了电商所需要的要素,与阿 ...

  9. 【转】cvs2svn 把CVS档案库转换为SVN档案库

    转载地址:http://jackdown.blog.sohu.com/66646130.html 在linux下的操作 1).安装 下载:Python 2.0   地址:http://www.pyth ...

  10. java 类加载顺序

    1.虚拟机在首次加载Java类时,会对静态初始化块.静态成员变量.静态方法进行一次初始化 2.只有在调用new方法时才会创建类的实例 3.类实例创建过程:按照父子继承关系进行初始化,首先执行父类的初始 ...