SSM integration Primary simple additions and deletions change

Source: Internet
Author: User
Tags aliases

1.jar Pack

  

2.MYBATIS-CONFIG.XM configuration file

<?xml version= "1.0" encoding= "UTF-8"? ><!DOCTYPE configurationpublic"-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><configuration> <!- -Custom Aliases-<typeAliases> <!--customizing aliases by type name-<!--<typealias type= "Cn.happy.entit Y.student "alias=" Student "/>-<!--the simple class name under the current specified package is aliased--< PackageName= "cn.happy.entity"/> </typeAliases> <environmentsdefault= "MySQL" > <environment id= "mysql" > <!--transactions with JDBC--<transactionmanager t Ype= "JDBC"/> <!--using your own connection pool--<datasource type= "pooled" > <propert Y name= "Driver" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "Jdbc:mysql://localhost:3 306/y2162 "/> <property name=" username "value=" root "/> <property name=" password "Value=" root "/> </dataSource> </environment> </environments> <mappers&gt        ; <mapper resource= "Cn/happy/dao/studentdao.xml"/> </mappers></configuration>

3.dao

 Public InterfaceIstudentdao { Public intAddstu (Student Stu)throwsIOException; //Delete    Public intDelstu (intIdthrowsIOException; //querying all Records    PublicList<student> FindAll ()throwsIOException; //Query Students ' collection by student's name    PublicList<student> findstudntbyname (Student Stu)throwsIOException;  PublicList<student> findstudntbyname (String stuname)throwsIOException; }

Daoimpl

 Public classStudentdaoimplImplementsIstudentdao {sqlsession session;  PublicStudentdaoimpl ()throwsIOException {Session=mybatisutil.getsession (); }     PublicList<student> findstudntbyname (String stuname)throwsIOException {List<Student> list = session.selectlist ("Findstudentbyname", Stuname);        Session.close (); returnlist; }         /*** * Fuzzy query*/         PublicJava.util.list<student> findstudntbyname (Student Stu)throwsIOException {List<Student> list = session.selectlist ("Findstudentbyname", Stu);        Session.close (); returnlist; }      /*** Check All*/     PublicJava.util.list<student> FindAll ()throwsIOException {List<Student> list = session.selectlist ("FindAll");        Session.close (); returnlist; }        /*** Delete*/     Public intDelstu (intIdthrowsIOException {intresult = Session.delete ("Delstudent", id);                 Session.commit ();        Session.close (); returnresult; }                 Public intAddstu (Student Stu)throwsIOException {intresult = Session.insert ("Insertstudent", Stu);             Session.commit ();        Session.close (); returnresult; }}

Dao.xml

<?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.happy.dao "> <insert id=" insertstudent "parametertype=" Student ">INSERT into student (stuname,stuage,studate) VALUES (#{stuname},#{stuage},#{studate})<!--SQL Server and MySQL only self-increment timing and insert timing: insert return self-increment Oracle first generates a self-increment before executing the insert--<selectkey keyproperty= "Stuno" resulttype= "int" >SELECT @ @identity</selectKey> </insert> <!--Remove students--<delete id= "Delstudent" >Delete from student where Stuno=#{xxx}<!--#{xxx} casually write, play a placeholder role-</delete> <!--All--<select id= "FindAll" resulttype= "Student" >Select*From student</select> <!--fuzzy query-<select id= "Findstudentbyname" resulttype= "Student" > <!--sel ECT * FROM student where stuname like concat ('% ', #{stuname}, '% ')--Select* FROM student where stuname like '%${value}% ' </select> </mapper>

MYBATISUITL Tool Class

  

/*** Tool class *@authorHappy **/ Public classMybatisutil {Private StaticString config= "Mybatis-config.xml"; StaticReader Reader; Static{        Try{Reader=resources.getresourceasreader (config); } Catch(IOException e) {e.printstacktrace (); }    }    Private StaticSqlsessionfactory factory =NewSqlsessionfactorybuilder (). build (reader); //provides a way to get to the session     Public StaticSqlsession getsession ()throwsioexception{System.out.println ("22222" +factory); //The disadvantage is that the factory is//What the hell did 1.1 opensession do?sqlsession session =factory.opensession (); System.out.println ("3333"); returnsession; }}

Single Test

  

 Public classMyTest {Istudentdao dao; @Before Public voidInitData ()throwsioexception{DAO=NewStudentdaoimpl (); }        /*** Fuzzy query *@throwsIOException*/@Test Public voidFindstudentbyname ()throwsioexception{/*Student stu=new Student (); Stu.setstuname ("three");*/List<Student> list = Dao.findstudntbyname ("three");  for(Student student:list) {System.out.println (Student.getstuname ()); }            }        /*** SelectAll Students *@throwsIOException*/@Test Public voidFindAll ()throwsioexception{List<Student> list =Dao.findall ();  for(Student student:list) {System.out.println (Student.getstuname ()); }            }                    /*** Delete Students *@throwsIOException*/@Test Public voidDelstudent ()throwsioexception{Dao.delstu (2); System.out.println ("OK"); } @Test Public voidTestadd ()throwsioexception{Student Stu=NewStudent (); Stu.setstuname ("Zhang San"); Stu.setstuage (21st); Stu.setstudate (NewDate ()); System.out.println ("Add before" +Stu); Istudentdao DAO=NewStudentdaoimpl ();                Dao.addstu (Stu); System.out.println (After "add" +Stu); }}

SSM integration Primary simple additions and deletions change

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.