Integration of MyBatis and SPRINGMVC (II.)

Source: Internet
Author: User

D. This time using the Oracle 11g database, the SQL script is built:

-------table :  Users-------------------------------------------------------------------------------------------------------------------- ---------------Drop table users;create table users (  userid number (10)  NOT NULL,  USERNAME VARCHAR2 ( not null,  birthday date)  default null,  sex char (1)  default 1 check  (sex in  (0,  1)),   address varchar2 ( NOT NULL,  DETAIL VARCHAR2) (1000)  default  ' No detail ',   score number (4,2)  default null,   primary key  (userId));comment on column users.sex is  ' 0: female 1 :  male ';/*--add comment-- comment on table users is  ' user table ';-- comment on  column users.userId is  ' user number ';-- comment on column users.sex is  ' 0: female 1: male ';--Modify the default values for the fields in the table   alter table users  modify  (Detail varchar2 ()  default  (' No detail ');   alter table  users modify  (Sex char (1)  default 1 check  (sex in  (0,  1));-- *//*--Create sequence sequencecreate sequence seq_useridminvalue 100100       --minimum nomaxvalue      --does not set the maximum value (determined by the machine), or   is set according to the value range of the table field   maxvaluestart with 100100    --counting starting from 1, variable value increment by 2  -- Add 1 each time, the value variable nocycle         --always accumulate, do not cycle nocache          --does not build a buffer, if the cache is established then the system will automatically read the cache value   SEQ, which will speed up the operation, if the machine or Oracle is dead, Then the SEQ value of the next read will be incoherent */create sequence seq_userid minvalue 100100 maxvalue 9999999999  start with 100100 increment by 1;--Creating a Trigger create or replace trigger tg_insertuserbefore  insert on users for each row when  (new.userid is null) begin   select seq_userid.nextval into:new.userid from dual;end;--Test:insert into  users  (username, birthday, sex, address, socre)  values  (' Oracle ',  to_ Date (' 1991-08-25 19:55:45 ',  ' yyyy-mm-dd hh24:mi:ss '), 1,  ' Shanghai ',  99.50);---- ---table :  Orders------------------------------------------------------------------------------------------------------------------- ----------------Drop table orders;create table orders (  orderid number (11)  not null,  user_id number (Ten)  not null,  orderNumber  VARCHAR2  not null,  primary key  (orderId),   CONSTRAINT&NBsp;fk_orders_users foreign key  (user_id)  references users  (userId)  on  Delete cascade);create sequence seq_orderid minvalue 1000000 maxvalue  99999999999 start with 1000000 increment by 1;create or replace  trigger tg_insertordersbefore insert on orders for each row when  (new.orderid is null) begin  select seq_orderid.nextval into:new.orderid  From dual;end;insert into orders values (seq_orderid.nextval, 100102,  ' ord100101 ' ); Insert into orders values (seq_orderid.nextval, 100106,  ' ord100102 ');insert  Into orders values (seq_orderid.nextval, 100108,  ' ord100103 '); insert into orders  values (seq_orderid.nextval, 100108,  ' ord100104 ');-------table :  Items-----------------------------------------------------------------------------------------------------------------------------------drop table  items;create table items (  itemid number)  not null,   Itemname varchar2 ( not null,  itemprice number) (11,2)  not null,   ITEMDETAIL VARCHAR2 ($)  not null,  primary key  (itemId)); Create sequence seq_itemid minvalue 1000000 maxvalue 99999999999 start  with 1000000 increment by 1;create or replace trigger tg_ insertitembefore insert on items for each row when  (New.itemId is  null) Begin  select seq_itemid.nextval into:new.itemid from dual;end;insert  into items values (seq_itemid.nextval,  ' milk ', 12.50,  ' good milk '); insert  into items values (seq_itemid.nextval,  ' car ', 65000.50,  ' Fast car '); Insert into items values (seq_ itemid.nextval,  ' australia steak ', 188.50,  ' delicious ');insert into items  VALUES (seq_itemid.nextval,  ' ua bag ', 350,  ' cool ');-------table :  OrderDetail-------------------------------------------------------------------------------------------------------------- ---------------------Drop table orderdetail;create table orderdetail (   Orderdetailid number (one)  not null,  order_id number (one)  not null,   item_id number (one)  not null,  item_number number (8)  not  Null,  item_price number (11,2)  not null,  primary key  ( Orderdetailid),  constraint fk_orderdetail_orders foreign key  (order_id)   references orders  (orderId),   constraint fk_orderdetail_items foreign key  (item_id)  references items   (itemId));create sequence seq_orderdetailid minvalue 1000000 maxvalue  99999999999 start with 1000000 increment by 1;create or replace  trigger tg_insertorderdetailbefore insert on orderdetail for each row  when  (new.orderdetailid is null) begin  select seq_orderdetailid.nextval  Into:new.orderdetailid from dual;end;insert into orderdetail values (seq_ orderdetailid.nextval, 1000001, 1000002, 1, 65000.50); insert into orderdetail  values (seq_orderdetailid.nextval, 1000003, 1000001, 3, 12.50);insert into  Orderdetail values (seq_orderdetailid.nextval, 1000002, 1000003, 1, 188.50); insert  into orderdetail values (seq_ORDERDETAILID.NEXTVAL, 1000004, 1000004, 2, 350);insert into orderdetail  Values (seq_orderdetailid.nextval, 1000004, 1000001, 1, 12.50);

E. Configure Mapper.xml and Mapper interface, this time only to configure a simple query Mapper.xml, more mapper.xml configuration please refer to my previous MyBatis blog.

Usermapper.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 "><mappernamespace=" Ssm.mapper.UserMapper ">  <!-- namespace: the  mapper.xml  the class path of the  Mapper  interface so that when the interface is called,,mybatis  will automatically find the   to be executed by  mapper  sql  statements   -->  <!--  Define sql  fragments, reusable  -->  <sql  Id= "Query_user_byuserid" >  <!--  Note: To do null  and   '   checksum  --><if  test= "value != null and value != " >   and  users.userid = #{value} </if>  </sql>    <!--   Dynamic  sql  query user information  -->  <select id= "Finduserbyid"  parametertype= "int " resulttype=" User ">  select * from users  <!-- where  can automatically handle the first  and -->  <where>    <include  refid= "Query_user_byuserid" ></include> </where>  </select></ Mapper>

Usermapper.java interface

Package Sam.mapper;import Sam.model.user;public Interface Usermapper {//query user public by User ID Finduserbyid (int userId) Throws Exception;}

F. MyBatis configuration is basically complete, the following write a test class to test whether MyBatis is good. Right-Usermapper.java new a Junit test case to modify the package to test package. And then next. Tick the method that needs to be tested in the Usermapper interface. As follows:

Usermappertest.java

package ssm.test;import java.io.inputstream;import org.apache.ibatis.io.resources;import  org.apache.ibatis.session.sqlsession;import org.apache.ibatis.session.sqlsessionfactory;import  org.apache.ibatis.session.sqlsessionfactorybuilder;import org.junit.before;import org.junit.test; import ssm.mapper.usermapper;import ssm.model.user;public class usermappertest { private sqlsessionfactory sqlsessionfactory; string resource =  "Mybatis/sqlmapconfig.xml"; @Beforepublic  void setup ()  throws  Exception {//  read configuration file via input stream inputstream inputstream =  Resources.getresourceasstream (Resource);//  through Sqlsessionfactorybuilder, Get Sqlsessionfactorysqlsessionfactory = new sqlsessionfactorybuilder (). Build (InputStream);} @Testpublic  void testfinduserbyid ()  {SqlSession sqlSession =  Sqlsessionfactory.opensession (); usermapper usermapper&Nbsp;= sqlsession.getmapper (Usermapper.class); User user = usermapper.finduserbyid (100101); SYSTEM.OUT.PRINTLN (user); Sqlsession.commit (); Sqlsession.close ();}}

Run the Test case and you should be able to query the User. At this point, the mybatis part is done. The following integrates spring.

Integration of MyBatis and SPRINGMVC (II.)

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.