開發環境:jvm 1.7.0_17eclipse(Juno ) + maven外掛程式( m2e - Maven Integration for Eclipse 1.4.0.20130601-0317)maven 3.0.4mysql 5.0.22-win32
1,項目的目錄結構 2 依賴的包(pom.xml檔案)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>MybatisLearning</groupId> <artifactId>MybatisLearning</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.28</version> </dependency> </dependencies></project>
3 建立一個configuration.xml檔案 放在 src/main/resources目錄下面
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <typeAliases> <!--給實體類起一個別名 user --> <typeAlias type="org.junjun.mybatis.bean.User" alias="User" /> </typeAliases> <!--資料來源配置 使用mysql資料庫 --> <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://127.0.0.1:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="" /> </dataSource> </environment> </environments> <mappers> <!--userMapper.xml裝載進來 同等於把“dao”的實現裝載進來 --> <mapper resource="mappers/UserMapper.xml" /> </mappers></configuration>
4 建立一個pojo類 User
package org.junjun.mybatis.bean;/** * 使用者類 * * @author db2admin * */public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public User() { }}
5 建立一個 UserMpapper的interface(不用實現)
package org.junjun.mybatis.dao;import org.junjun.mybatis.bean.User; public interface UserMapper { public User findByName(String name); public void insertUser(User user); }
6建立一個UserMapping.xml 檔案在 src/main/resources/mappers/目錄下
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--這塊等於dao介面的實現 namespace必須和介面的類路徑一樣--><mapper namespace="org.junjun.mybatis.dao.UserMapper"> <!-- findByName 和 inserUser必須和介面中的方法名一樣 返回一個User 就是剛才的別名 如果不弄別名要連類路徑一起寫--> <select id="findByName" parameterType="HashMap" resultType="User"> select * from user where name=#{name} </select> <insert id="insertUser" parameterType="org.junjun.mybatis.bean.User"> insert into user (name,age) value ( #{name,jdbcType=VARCHAR} , #{age,jdbcType=SMALLINT} ) </insert></mapper>
7 sql.txt中的sql語句如下 用來建立一個表和插入一條資料
create database mybatis
create table user( name varchar(255), age int(11));insert into user value ("andy",22)
8 在mysql中建立一個資料庫 mybatis 建立表user 並插入一條資料(“andy”,22)建立一個資料庫create database mybatis
建立一個表usercreate table user( name varchar(255), age int(11));
插入一行資料insert into user value ("andy",22) 效果如下:(使用mysql-font)
8 寫一個測試類別MyBatisTest
package org.junjun.mybatis.test;import java.io.IOException;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.junjun.mybatis.bean.User;import org.junjun.mybatis.dao.UserMapper;/** * myBatis資料庫測試 * */public class MyBatisTest { /** * 獲得MyBatis SqlSessionFactory * SqlSessionFactory負責建立SqlSession,一旦建立成功,就可以用SqlSession執行個體來執行映射語句,commit,rollback,close等方法。 * @return */ private static SqlSessionFactory getSessionFactory() { SqlSessionFactory sessionFactory = null; String resource = "configuration.xml"; try { sessionFactory = new SqlSessionFactoryBuilder().build(Resources .getResourceAsReader(resource)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return sessionFactory; } public static void main(String[] args) { SqlSession sqlSession = getSessionFactory().openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // test select User user = userMapper.findByName("andy"); System.out.println(user.getName()); // test insert User user1 = new User(); user1.setName("xl"); user1.setAge(22); userMapper.insertUser(user1); //it is a must or no data will be insert into server. sqlSession.commit(); }}
9 運行結果eclipse運行結果mysql-font 結果
10 原始碼下載MybatisLearning.zip:http://download.csdn.net/detail/skywalkervvv/6963517