Mybatis入門執行個體

來源:互聯網
上載者:User

標籤:class   blog   code   java   http   ext   

第一步:建立項目匯入jar包


第二步:建表:

CREATE TABLE `person` (  `ID` int(10) NOT NULL AUTO_INCREMENT,  `NAME` varchar(10) DEFAULT NULL,  `GENDER` int(10) DEFAULT NULL,  `ADRESS` varchar(50) DEFAULT NULL,  `BIRTHDAY` date DEFAULT NULL,  PRIMARY KEY (`ID`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

第二步:建立實體類

package com.hao947.model;import java.util.Date;public class Person {private Integer id;private String name;private Integer gender;private String address;private Date birthday;@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + ", gender=" + gender+ ", address=" + address + ", birthday=" + birthday + "]";}}

第四步:建立設定檔

<?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><typeAliases><!-- 自訂別名:type:要定義的資料類型alias:別名的名字非自訂別名:規則:別名jdk提供的所有的類的名字不區分大小寫,如果是封裝類那麼直接使用其基本類型也可以 --><typeAlias type="com.hao947.model.Person" alias="person"/></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/mybatis" /><property name="username" value="root" /><property name="password" value="hao947" /></dataSource></environment></environments><!-- 集中管理庫表的對應檔 --><mappers><mapper resource="com/hao947/sql/mapper/PersonMapper.xml" /></mappers></configuration>

第五步:庫表的mapping對應檔

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace:當前庫表對應檔的命名空間,唯一的不能重複 --><mapper namespace="com.hao947.sql.mapper.PersonMapper"><!-- id:當前sql的唯一標識parameterType:輸入參數的資料類型resultType:傳回值的資料類型#{}:用來接受參數的,如果是傳遞一個參數#{id}內容任意,如果是多個參數就有一定的規則,採用的是先行編譯的形式select * from person p where p.id = ? ,安全性很高 --><select id="selectPersonById" parameterType="java.lang.Integer" resultType="com.hao947.model.Person">select * from person p where p.id = #{id}</select></mapper>

第六步:測試代碼

package com.hao947.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 com.hao947.model.Person;public class PersonTest {SqlSessionFactory sqlSessionFactory;@Beforepublic void setUp() throws Exception {// 讀取資源流InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");// 初始化session工廠sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);}@Testpublic void selectPersonById() {// 建立一個sqlsessionSqlSession session = sqlSessionFactory.openSession();try {Person p = session.selectOne("com.hao947.sql.mapper.PersonMapper.selectPersonById", 1);System.out.println(p);} finally {session.close();}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.