请注意后面文章中的注释
MyBatis官方入门:http://mybatis.github.io/mybatis-3/zh/getting-started.html
源代码下载:(正在审核中)
1. config:配置文件目录 2. pojo:Bean类包 3. controller:数据接口 4. sql:数据库源文件
CREATE DATABASE IF NOT EXISTS `mybatis`; USE `mybatis`; DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(45) DEFAULT NULL, `age` int(11) DEFAULT NULL, `phone` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; LOCK TABLES `users` WRITE; INSERT INTO `users` VALUES (1,‘jimmy.song‘,22,‘13810001234‘),(2,‘sunspot‘,24,‘13812341234‘); UNLOCK TABLES;
位置:cc/fozone/demo/mybatis/config/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="org.gjt.mm.mysql.Driver"/> <!-- 数据库链接 --> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?charset=utf8"/> <!-- 数据库帐号密码 --> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!-- 与数据库映射的配置文件列表 --> <mappers> <mapper resource="cc/fozone/demo/mybatis/config/mapper/User.mapper.xml"/> </mappers> </configuration>
位置:cc/fozone/demo/mybatis/pojo/User.java
package cc.fozone.demo.mybatis.pojo; public class User { private int id; private String name; private int age; private String phone; 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String toString(){ return "{id:"+id+",name:"+name+",age:"+age+",phone:"+phone+"}"; } }
位置:cc/fozone/demo/mybatis/controller/IUserController.java
package cc.fozone.demo.mybatis.controller; import java.util.List; import cc.fozone.demo.mybatis.pojo.User; public interface IUserController { // 查询所有的用户 public List<User> getAllUsers(); // 根据电话查询用户 public User getUser(String phone); }
位置:cc/fozone/demo/mybatis/config/mapper/User.mapper.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"> <!-- namespace:与Controller接口匹配 --> <mapper namespace="cc.fozone.demo.mybatis.controller.IUserController"> <!-- User表映射 type:类地址 id:实例化ID --> <resultMap type="cc.fozone.demo.mybatis.pojo.User" id="userResultMap"> <id property="id" column="id" /> <result property="name" column="name" /> <result property="age" column="age"/> <result property="phone" column="phone"/> </resultMap> <!-- select 查询语句 id:唯一标示符,与Controller的接口方法一致 resultMap: 查询的数据实例化注入的对象标示引用 --> <select id="getAllUsers" resultMap="userResultMap"> <![CDATA[ select id,name,age,phone from users ]]> </select> <!-- #{phone}:参数,与Controller中的getUser方法中的参数一致 resultType:查询的数据库实例化注入的类型,与resultMap不能同时使用,但建议使用resultMap --> <select id="getUser" resultType="cc.fozone.demo.mybatis.pojo.User"> <![CDATA[ select id,name,age,phone from users where phone = #{phone} ]]> </select> </mapper>
位置:cc/fozone/demo/mybatis/App.java
package cc.fozone.demo.mybatis; import java.io.IOException; import java.io.InputStream; import java.util.List; 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 cc.fozone.demo.mybatis.controller.IUserController; import cc.fozone.demo.mybatis.pojo.User; /** * Hello world! * */ public class App { // 数据库链接配置 private static final String CONFIG = "cc/fozone/demo/mybatis/config/mybatis.config.xml"; // SQL会话工厂 private SqlSessionFactory factory; public App() { // 初始化 this.init(); } // 初始化SQL会话工厂 private void init() { InputStream input; try { input = Resources.getResourceAsStream(CONFIG); this.factory = new SqlSessionFactoryBuilder().build(input); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 查询所有用户列表 public List<User> getUsers() { // 打开会话 SqlSession session = this.factory.openSession(); // 根据映射实例化控制器对象,这里的IUserController.class与Mapper配置中的namespace一致 IUserController controller = session.getMapper(IUserController.class); // 获得所有用户 List<User> list = controller.getAllUsers(); // 关闭会话 session.close(); return list; } // 根据电话号查询用户 public User getUser(String phone) { User user = null; // 会话 SqlSession session = this.factory.openSession(); IUserController controller = session.getMapper(IUserController.class); // 查询用户,这里传入的参数会被替换在Mapper中的#{phone}占位符 user = controller.getUser(phone); session.close(); return user; } }
位置:cc/fozone/demo/mybatis/AppTest.java
package cc.fozone.demo.mybatis; import java.util.List; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; import cc.fozone.demo.mybatis.pojo.User; public class AppTest { private App app; @Before public void setUp() throws Exception { app = new App(); assertNotNull(app); } @Test public void testList() { List<User> list = app.getUsers(); assertEquals(2,list.size()); } @Test public void testUser(){ User user = app.getUser("13810001234"); assertNotNull(user); assertEquals("jimmy.song", user.getName()); } }
AppTest.java文件鼠标右键 -> Run -> JUnit Test
本文出自 “博远至静” 博客,请务必保留此出处http://sunspot.blog.51cto.com/372554/1554965
原文地址:http://sunspot.blog.51cto.com/372554/1554965