eclipse + jdk 1.8 + mybatis html
一、數據庫準備java
安裝mysql數據庫,創建數據庫test,在test庫下創建測試的表mysql
CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `pass` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;
INSERT INTO `t_user` VALUES ('1', 'ssssssssssssssss', 'ddsssssssdd');
二、建立maven工程sql
(1)使用eclipse建立maven工程,設置pom.xml:添加mybatis和mysql的驅動,並設置jdk版本數據庫
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> </dependencies> <build> <plugins> <!-- compiler插件, 設定JDK版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <!-- 源代碼使用的開發版本 --> <target>1.8</target> <!-- 須要生成的目標class文件的編譯版本 --> <!-- 通常而言,target與source是保持一致的,可是,有時候爲了讓程序能在其餘版本的jdk中運行(對於低版本目標jdk,源代碼中須要沒有使用低版本jdk中不支持的語法),會存在target不一樣於source的狀況 --> </configuration> </plugin> </plugins> </build>
在工程目錄上,右鍵菜單選擇「Maven」 ,「Update Project」 ,更新此工程,使設置生效。apache
(2)建立resources資源文件夾session
在工程目錄上,右鍵菜單"New",「Source Folder」 ,新建資源文件目錄resourcesmybatis
三、建立與t_user表對應的Java Bean對象app
在src/test下創建包com.test,在該包下創建類Usereclipse
package com.test; public class User { private int id; private String name; private String pass; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } }
四、在resources目錄下建立mybatis-config.xml文件,內容以下:
<?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> <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/test?characterEncoding=utf-8&serverTimezone=UTC" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <!--這個文件下面將建立 --> <mapper resource="UserMapper.xml" /> </mappers> </configuration>
五、在resources目錄下建立UserMapper.xml文件,內容以下:
<?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"> <mapper namespace="User"> <!--resultType 表示com.test.User這樣一個實體對象 --> <select id="selectUser" parameterType="int" resultType="com.test.User"> select * from t_user where id = #{id} </select> </mapper>
六、在com.test下建立測試類Test,內容以下:
package com.test; import java.io.IOException; 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; public class Test { static String resource = "mybatis-config.xml"; public static void main(String[] args) throws IOException { InputStream inputstream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputstream); SqlSession session = sqlSessionFactory.openSession(); try { User user = session.selectOne("User.selectUser", 1); System.out.println("user.getName=" + user.getName()); } finally { session.close(); } } }
總體關係以下:
錯誤及處理:
一、mysql驅動的URL中須要添加serverTimezone=UTC參數,不然可能會報時區錯誤;
xml可能會把&符號做爲特殊符號處理,須要用&代替,才能解讀爲&;
參考:
mybatis – MyBatis 3 | 簡介 http://www.mybatis.org/mybatis-3/zh/index.html
MyBitis(iBitis)系列隨筆之一:MyBitis入門實例 - CSDN博客 http://blog.csdn.net/jefry_xdz/article/details/8755974
關於mysql-connector-java(JDBC驅動)的一些坑 - Silence.Sky - 博客園 http://www.javashuo.com/article/p-xrhsfpsc-bk.html