难得清闲,学习了一下mybatis持久化框架,在这里分享一下。
mybatis-3.1.1.jar(核心包)
mysql-connector-java-5.1.17-bin.jar
junit.jar (junit)
org.hamcrest.core_1.1.0.v20090501071000.jar (junit)
提供数据库连接信息+实体映射文件,和hibernate的主配置文件类似
<?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> <span style="white-space:pre"> </span><!-- 读取数据库连接属性文件 --> <properties resource="jdbc.properties" /> <!-- 确定包的地址 --> <typeAliases> <package name="org.tgb.model" /> </typeAliases> <!-- 配置开发环境,默认的都是这个情况 --> <environments default="development"> <environment id="development"> <!-- 使用jdbc的事务管理 --> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <!-- 将mapper文件加入到配置文件中 --> <mappers> <mapper resource="org/tgb/model/User.xml" /> </mappers> <span style="white-space:pre"> </span></configuration>
username=root password=root url=jdbc:mysql://localhost:3306/itat_shop driver=com.mysql.jdbc.Driver
public class User { private String username; private String password; private String type; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
<?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="itat.zttc.shop.model.User"> <insert id="add" parameterType="User"> insert into t_user (username,password,type) value(#{username},#{password},#{type}) </insert> <update id="update" parameterType="User"> update t_user set password=#{password},username=#{username},type=#{type} where username=#{username} </update> <delete id="delete" parameterType="String"> delete from t_user where username=#{username} </delete> <select id="load" parameterType="int" resultType="User"> select * from t_user where id=#{id} </select> <select id="list" resultType="User"> select * from t_user </select> </mapper>
public class TestMybatis { @Test public void testAdd() { try { // 1、创建配置文件(mybatis-config.xml)的输入流 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); // 2、创建SQLSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); // 3、创建SQLSessioin SqlSession session = factory.openSession(); // 4、调用mapper文件插入数据(调用之前需要将mapper文件加入到mybatis-config.xml中) User u = new User(); u.setPassword("Mybatis2"); u.setType("Mybatis2"); u.setUsername("Mybatis2"); session.insert("itat.zttc.shop.model.User.add", u); System.err.println("call the insert method"); session.commit(); session.close(); } catch (IOException e) { e.printStackTrace(); } <span style="white-space:pre"> </span>} @Test public void testdelete() { try { // 1、创建配置文件(mybatis-config.xml)的输入流 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); // 2、创建SQLSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); // 3、创建SQLSessioin SqlSession session = factory.openSession(); // 4、调用mapper文件插入数据(调用之前需要将mapper文件加入到mybatis-config.xml中) User u = new User(); u.setUsername("Mybatis2"); session.delete("itat.zttc.shop.model.User.delete", u.getUsername()); System.err.println("call the delete method"); session.commit(); session.close(); } catch (IOException e) { e.printStackTrace(); } } @Test public void testUpdate() { try { <span style="white-space:pre"> </span>// 1、创建配置文件(mybatis-config.xml)的输入流 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); // 2、创建SQLSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); // 3、创建SQLSessioin SqlSession session = factory.openSession(); // 4、调用mapper文件插入数据(调用之前需要将mapper文件加入到mybatis-config.xml中) User u = new User(); u.setPassword("Mybatis"); u.setType("Mybatis"); u.setUsername("Mybatis"); session.update("itat.zttc.shop.model.User.update", u); System.err.println("call the update method"); session.commit(); session.close(); } catch (IOException e) { e.printStackTrace(); } } @Test public void testSelect() { try { // 1、创建配置文件(mybatis-config.xml)的输入流 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); // 2、创建SQLSessionFactory SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); // 3、创建SQLSessioin SqlSession session = factory.openSession(); // 4、调用mapper文件插入数据(调用之前需要将mapper文件加入到mybatis-config.xml中) User u = new User(); u.setUsername("Mybatis"); List<User> userList = session.selectList("itat.zttc.shop.model.User.list"); for (User u2:userList) { System.out.println(u2.getUsername()); } session.commit(); session.close(); } catch (IOException e) { e.printStackTrace(); } } }
(未完,待续)