发现一个MyBatis博客,讲的挺好的:http://legend2011.blog.51cto.com/3018495/908956
官方教程:http://mybatis.github.com/mybatis-3/zh/sqlmap-xml.html
官方的教程是中文的,讲的很好,一定要看啊。
MyBatis 真正的力量是在映射语句中。这里是奇迹发生的地方。对于所有的力量,SQL映射的 XML 文件是相当的简单。当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大约 95%的代码量。MyBatis 的构建就是聚焦于 SQL 的,使其远离于普通的方式。
SQL 映射文件有很少的几个顶级元素(按照它们应该被定义的顺序):
二.示例
这里就简单的实现以下增删改查:
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="org.ygy.mapper.UserMapper"> <!-- 插入,参数是User对象,使用自动增长的ID --> <insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id"> <!-- 就是普通的插入语句,这里使用#{xxx}的方式赋值,这里的xxx要和实体类中的属性对应 --> insert into t_user(name , password , email , gender , age) values(#{name} , #{password} , #{email} , #{gender} , #{age}) </insert> <!-- 更新用户信息 --> <update id="update" parameterType="User"> update t_user set name=#{name},password=#{password},email=#{email},gender=#{gender},age=#{age} where id=#{id} </update> <!-- 根据用户ID,删除一个用户 --> <delete id="delete" parameterType="int"> delete from t_user where id=#{id} </delete> <!-- 查询所有用户 --> <select id="selectUser" resultType="User"> select * from t_user </select> <!-- 根据用户ID,查询用户 --> <select id="queryById" parameterType="int" resultType="User"> select * from t_user where id=#{id} </select> <!-- 根据用户名,查找用户 --> <select id="queryByName" parameterType="String" resultType="User"> select *from t_user where name=#{name} </select> </mapper>
package org.ygy.dao; import java.util.List; import org.ygy.model.User; /** * User类Dao接口 * @author yuguiyang * */ public interface UserDao { /** * 添加一个用户 * @param user * @return */ public int insert(User user); /** * 修改用户信息 * @param user * @return */ public int update(User user); /** * 修改用户 * @param id * @return */ public int delete(int id); /** * 获取所有用户 * @return */ public List<User> selectUser(); /** * 根据用户ID查找用户 * @param id * @return */ public User queryById(int id); /** * 根据用户名查找用户 * @param name * @return */ public User queryByName(String name); }UserDaoImpl.java
package org.ygy.dao.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.ygy.dao.UserDao; import org.ygy.model.User; import org.ygy.util.MyBatisUtil; /** * User类Dao实现类 * @author yuguiyang * */ public class UserDaoImpl implements UserDao { @Override public int insert(User user) { //获取session SqlSession session = MyBatisUtil.getSession(); //调用UserMapper.xml中的insert,并返回受影响的行数 int affectedRows = session.insert("org.ygy.mapper.UserMapper.insert" , user); //提交事务,执行insert,update,delete时,一定要提交,不然数据库中不会修改 session.commit(); //关闭session session.close(); return affectedRows; } @Override public int update(User user) { SqlSession session = MyBatisUtil.getSession(); int affectedRows = session.update("org.ygy.mapper.UserMapper.update" , user); session.commit(); session.close(); return affectedRows; } @Override public int delete(int id) { SqlSession session = MyBatisUtil.getSession(); int affectedRows = session.delete("org.ygy.mapper.UserMapper.delete" , id); session.commit(); session.close(); return affectedRows; } @Override public List<User> selectUser() { SqlSession session = MyBatisUtil.getSession(); List<User> userList = session.selectList("org.ygy.mapper.UserMapper.selectUser"); session.commit(); session.close(); return userList; } @Override public User queryById(int id) { SqlSession session = MyBatisUtil.getSession(); User user = session.selectOne("org.ygy.mapper.UserMapper.queryById" , id); session.commit(); session.close(); return user; } @Override public User queryByName(String name) { SqlSession session = MyBatisUtil.getSession(); User user = session.selectOne("org.ygy.mapper.UserMapper.queryByName" , name); session.commit(); session.close(); return user; } }
UserDaoTest.java
package org.ygy.dao; import static org.junit.Assert.assertEquals; import java.util.List; import org.junit.Before; import org.junit.Test; import org.ygy.dao.impl.UserDaoImpl; import org.ygy.model.User; public class UserDaoTest { private UserDao userDao = null; @Before public void before() { userDao = new UserDaoImpl(); } @Test public void testInsert() { User user = new User(); user.setName("乔巴"); user.setPassword("qiaoba"); user.setAge(23); user.setGender(0); user.setEmail("[email protected]"); assertEquals(1 , userDao.insert(user)); } @Test public void testQueryByName() { User user = userDao.queryByName("乔巴"); System.out.println("user->" + user); } @Test public void testQueryById() { User user = userDao.queryById(4); System.out.println("user->" + user); } @Test public void testUpdate() { User user = userDao.queryByName("乔巴"); user.setName("七段变身-乔巴"); userDao.update(user); } @Test public void testDelete() { assertEquals(1 , userDao.delete(3)); } @Test public void testSelectUser() { List<User> userList = userDao.selectUser(); for(User each : userList) { System.out.println("each->" + each); } } }