Mybatis+struts2+spring Integration

Source: Internet
Author: User

Transform the student project into SSM Struts2 +mybatis+spring

1, first add Spring support: Class Library Three, Applicationcontext.xml write in Webinf under four namespaces, listener

2, add struts2 support STRUTS2 with spring integrated jar package

3, add MYBATIS2 Support, import jar package, MyBatis jar package with spring integration, The original in the Mybatis.cfg.xml most of the configuration is written in Applicationcontext.xml, like Hibernate, just like the log output and paging plug-ins still need to write in 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.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/context/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/CONTEXT/SPRING-CONTEXT-3.1.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-3.1.xsd "><context:component-scan base-package=" Cn.bdqn.student "/><!--Configuring the data source--><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"/><!--configuration Sqlsessionfactory "DataSource" injection data source typealiasespackage All of this entity class is automatically set alias--><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 "/><!--configuration transaction manager--><bean id=" TransactionManager "class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager "p:datasource-ref=" DataSource "/><!--Transaction enhancement --><tx:advice id= "Txadvice" ><tx:attributes><tx:method name= "*"/></tx:attributes></ tx:advice><!--transaction facets--><aop:config><aop:pointcut expression= "Execution (* cn.bdqn.student.service .. *.*(..))" Id= "Txmethods"/><aop:advisor advice-ref= "Txadvice" pointcut-ref= "Txmethods"/></aop:config><!-- Configure the dynamically created Mapper implementation class object--><!--<bean id= "Usermapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" P: Mapperinterface= "Cn.bdqn.student.mapper.UserMapper" p:sqlsessionfactory-ref= "sqlsessionfactory"/> <bean i D= "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"/>--><!--scans the specified package, automatically generates the Mapper implementation class object based on the mapping--><bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" P: Basepackage= "Cn.bdqn.student.mapper"/></beans>

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE mapperpublic "-//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.picfrom Student s INNER JO In Course C on s.cid=c.id INNER joins education e on S.eid=e.idwhere 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.T IME, S.state, S.addtimE, c.name as CNAME, e.name as ename, S.picfrom Student s INNER JOIN Course C on s.cid=c.id INNER JO In Education e-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"/><associationProperty= "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 setname=#{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") the Integer State);p ublic void Savestudent (Student Student); Public Student getstudent (Integer ID);//update student public void Updatestudent (Student Student);//delete 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; @Autowiredpublic void Setstudentmapper ( Studentmapper studentmapper) {this.studentmapper = Studentmapper;} @Overridepublic void Savestudent (Student Student) {studentmapper.savestudent (Student);} @Overridepublic 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;} Because name is an empty string, but I judge it by empty, so I need to deal with it.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;} @Overridepublic Student getstudent (Integer ID) {//TODO auto-generated method Stubreturn studentmapper.getstudent (ID);} @Overridepublic void Updatestudent (Student Student) {//TODO auto-generated method stub} @Overridepublic void Deletestudent (Integer ID) {//TODO auto-generated method stub}}

Mybatis+struts2+spring Integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.