Learning MyBatis So many days, so I give you to share my study results. Configuration from the most basic start.
First, create a Web project and look at the project schema
Second, said the project will think of the need for what jar
Three, is to prepare the large configuration link ORCL database
<?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> <!--alias Customization--<typeAliases> <!--customizing aliases by type name--< ;! --<typealias type="cn.happy.entity.Student"alias="Student"/>-<!--the simple class name under the current specified package is an alias--<package name="cn.happy.entity"/> </typeAliases> <environmentsdefault="Development"> <environment id="Development"> <!--using jdbc--<transactionmanager type="JDBC"/> <!--using your own connection pool-<datasource type="Pooled"> <property name="Driver"Value="Oracle.jdbc.driver.OracleDriver"/> <property name="URL"Value="Jdbc:oracle:thin: @localhost: 1521:orcl"/> <property name="username"Value="T2"/> <property name="Password"Value="T2"/> </dataSource> </environment> </environments> <mappers> <ma Pper resource="Cn/happy/dao/studentdao.xml"/> </mappers></configuration>
Iv. Preparing small configurations
<?xml version="1.0" encoding="UTF-8" ? ><! "-//mybatis.org//dtd Mapper 3.0//en" "http://mybatis.org/ DTD/MYBATIS-3-MAPPER.DTD"namespace="Cn.happy.dao.IStudentDAO ">
Additions </mapper> Changes
Five, prepare the tool class
PackageCn.happy.util;Importjava.io.IOException;ImportJava.io.Reader;Importorg.apache.ibatis.io.Resources;Importorg.apache.ibatis.session.SqlSession;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.apache.ibatis.session.SqlSessionFactoryBuilder;/*** 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; }}
VI. Preparation of entity classes
Packagecn.happy.entity;Importjava.util.Date;/*** Student Entity class *@authorHappy **/ Public classStudent {PrivateInteger Stuno; PrivateString Stuname; PrivateInteger Stuage; PrivateDate studate; PublicString toString () {return"Student [stuno=" + Stuno + ", stuname=" + Stuname + ", stuage=" + Stuage + ", studate=" + Studate + "]";} PublicInteger Getstuno () {returnStuno;} Public voidSetstuno (Integer stuno) { This. Stuno =Stuno;} PublicString Getstuname () {returnStuname;} Public voidsetstuname (String stuname) { This. Stuname =Stuname;} PublicInteger getstuage () {returnstuage;} Public voidsetstuage (Integer stuage) { This. Stuage =stuage;} PublicDate getstudate () {returnstudate;} Public voidsetstudate (Date studate) { This. studate =Studate;} }
Seven, the following is the specific code additions and deletions to change the front is public
(a) The following implementation to see all
1 Defining methods in interfaces
// querying all records Public throws IOException;
2 =======> is not required to implement the class due to dynamic loading in the configuration
<!--check All- <select id= "FindAll" resulttype= "Student" > * from Student </select>
3 in the test class
Istudentdao DAO; @Before Public voidInitData ()throwsioexception{sqlsession Session=mybatisutil.getsession (); //Dynamic kick Out implementation class//first change studentdao.xml to <mapper namespace= "Cn.happy.dao.IStudentDAO" >Dao=session.getmapper (Istudentdao.class); }/*** SelectAll Students *@throwsIOException*/@Test Public voidFindAll ()throwsioexception{List<Student> list =Dao.findall (); for(Student student:list) {System.out.println (Student.getstuname ()); } }
(b) Here are the additions
1 Defining methods in interfaces
// Add Public int throws IOException;
2 =======> is not required to implement the class due to dynamic loading in the configuration
<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 returns self-increment Oracle first generates a self-increment before performing the insert -to- <selectkey keyproperty= "Stuno" resulttype= " int "> <!--mysql-- SELECT @ @identity <!--ORCL--- Select Sql_ Num.currval from dual </selectKey> </insert>
3 in the test class
@Test Public void throws ioexception{ Student stu=new Student (); Stu.setstuname ("Y2161 Good Man"); Stu.setstuage (+); Stu.setstudate (new Date ()); System.out.println ("add before =======" +stu); Istudentdao DAO=new Studentdaoimpl (); Dao.addstu (Stu); System.out.println ("Add =======" +stu);
(b) The following is the deletion
1 Defining methods in interfaces
// Delete Public int delstu (intthrows IOException;
2 =======> is not required to implement the class due to dynamic loading in the configuration
<!--Remove students-- <delete id="delstudent"> fromwhere stuno=#{xxx}<!--#{xxx} to write, play a placeholder role- </delete>
3 in the test class
/** * Delete student @throws ioexception */ @Test publicvoidthrows ioexception{ dao.delstu (2); System.out.println ("OK"); }
(b) The following is a fuzzy query
1 Defining methods in interfaces
// Query Students ' collection by student's name (one is an entity one is a string actually can) Public throws IOException; Public throws IOException;
2 =======> is not required to implement the class due to dynamic loading in the configuration
<!--fuzzy query mysql database-- <select id= "findstudentbyname" resulttype= "Student" > < !-- SELECT * from student where stuname like concat ('% ', #{stuname}, ' % ')---* FROM Student where stuname like '%${value}% ' </select>
3 in the test class
/** * Fuzzy query * @throws IOException */ @Test public voi D findstudentbyname () throws ioexception{ /* student stu=new Student (); Stu.setstuname ("person"); */ list <Student> list = dao.findstudntbyname ("person" ); for (Student student:list) {SYSTEM.O Ut.println (Student.getstuname ()); } }
MyBatis Framework's fuzzy query (multiple notation), delete, add