最近研究了一些MyBatis技术,虽然工作中还未用到,但是觉得了解一下也是不错的.这里记录了第一个简单的Demo程序,防止自己忘记.
第一步需要配置Mybatis-config.xml文件.注意:这里使用了2种方式对实体类进行了映射.在<Mapper>中,第一个User对象用XMl格式进行映射,第二个Category用注解CategoryMappper格式进行映射.
<?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> <properties resource="jdbc.properties" ></properties> <!-- <typeAliases> <package name="com.mybatis02.model"/> </typeAliases> --> <environments default="devEnvironment"> <environment id="devEnvironment"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${dirver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <!-- 利用XML格式对User对象进行映射 --> <mapper resource="com/mybatis02/model/User.xml"/> <!-- 用注解模式对Category进行映射 --> <mapper class="com.mybatis02.mapper.CategoryMapper"/> </mappers> </configuration>
第二步对mybatis-config.xml进行解析
public class MyBatisUtil { private static SqlSessionFactory factory; static{ try { InputStream in = Resources.class.getResourceAsStream("mybatis-config.xml"); factory = new SqlSessionFactoryBuilder().build(in); } catch (Exception e) { e.printStackTrace(); } } public static SqlSession createSession(){ return factory.openSession(); } public static void closeSession(SqlSession session){ if (session != null) { session.close(); } } }
下面分2中方式,对xml格式和注解方式分别进行介绍:
XML格式:
1.1.创建实体类User(这里省略了get和set方法)
public class User { private int id; private String username; private String password; private String nickname; private int type; private List<Address> addresses; ... }
1.2.创建User对象的xml映射文件.这里需要注意:namespace属性,需要配置User类的全路径.定义了一个id为add的Insert操作.其参数类型(parameterType)为一个User对象
<?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="com.mybatis02.model.User"> <insert id="add" parameterType="com.mybatis02.model.User"> insert t_user(id,username,password,nickname,type) values(#{id},#{username},#{password},#{nickname},#{type}) </insert> </mapper>
1.3.对XML映射方式进行测试
@Test public void testAdd() { // 1.创建Mybatis配置文件输入流 InputStream in = null; try { in = Resources.getResourceAsStream("mybatis-config.xml"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 2.创建SqlSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); // 3.创建SQlSession SqlSession session = factory.openSession(); // 4.调用UserMapper文件,将对象插入数据库(在调用文件之前,需要将mapper文件加入到mybatis-config.xml中) User user = new User(); user.setId(8); user.setUsername("张飞"); user.setPassword("123"); user.setNickname("张三"); user.setType(1); session.insert("com.mybatis02.model.User.add", user); session.commit(); session.close(); }
对注解映射方式进行demo的编写.
2.1.同样创建实体类
public class Category { private int id; private String name; 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; } }
2.2 创建CategoryMapper映射类 创建了一个add方法,在方法上添加Insert操作,执行对应的sql.
这个文件在第一步中的mybatis-config.xml中引入.
<mapper class="com.mybatis02.mapper.CategoryMapper"/>
public interface CategoryMapper { @Insert("insert into t_category(id,name) value (#{id},#{name})") public void add(Category category); }
2.3 测试
@Test public void testCategory() { // 1.创建Mybatis配置文件输入流 InputStream in = null; try { in = Resources.getResourceAsStream("mybatis-config.xml"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 2.创建SqlSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); // 3.创建SQlSession SqlSession session = factory.openSession(); // 4.调用CategoryMapper接口,将对象插入数据库(在调用文件之前,需要将mapper文件加入到mybatis-config.xml中) Category category = new Category(); category.setId(1); category.setName("衣服"); session.insert("com.mybatis02.mapper.CategoryMapper.add", category); session.commit(); session.close(); }
感触:最开始写demo,报了一个错误,原来我把xml方式映射和注解方式映射弄混了.两种方式各取一半,报了各种错误.
以后遇到问题,需要一步一步的推敲.保持逻辑的清晰,才能更加准确的定位错误,及时发现和修正错误.
原文:http://www.cnblogs.com/Cilimer/p/3911560.html