接着前一篇继续说
1.创建了test2包与util包
2.MybatisUtils类的代码
package com.kpzc.util; import java.io.InputStream; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.kpzc.test1.Test; /**返回factory * @author kpzc * */ public class MybatisUtils { public static SqlSessionFactory getFactory() { String resource = "conf.xml"; InputStream is = Test.class.getClassLoader().getResourceAsStream( resource); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); return factory; } }
3.mapper文件
<?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"> <!-- 一般写包的路径(不带.xml) --> <mapper namespace="com.kpzc.test2.userMapper"> <!-- 根据id查询得到一个user对象 parameterType id的参数类型 resultType 写bean对象 --> <select id="getUser" parameterType="int" resultType="com.kpzc.bean.User"> select * from users where id=#{id} </select> <insert id="addUser" parameterType="com.kpzc.bean.User"> insert into users(name, age) values(#{name}, #{age}) </insert> <delete id="deleteUser" parameterType="int"> delete from users where id=#{id} </delete> <update id="updateUser" parameterType="com.kpzc.bean.User"> update users set name=#{name},age=#{age} where id=#{id} </update> <select id="getAllUsers" resultType="com.kpzc.bean.User"> select * from users </select> </mapper>
4.Test2.java(一个个测试,别冲突了)
package com.kpzc.test2; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; import com.kpzc.bean.User; import com.kpzc.util.MybatisUtils; /** * @author kpzc * */ public class Test2 { /*@Test public void testAdd() { SqlSessionFactory factory = MybatisUtils.getFactory(); SqlSession session = factory.openSession(); String statement = "com.kpzc.test2.userMapper.addUser"; int insert = session.insert(statement , new User(-2, "王者农药", 3)); //提交 session.commit(); session.close(); System.out.println(insert); } */ /*@Test public void testUpate() { SqlSessionFactory factory = MybatisUtils.getFactory(); SqlSession session = factory.openSession(true); String statement = "com.kpzc.test2.userMapper.updateUser"; int update = session.update(statement, new User(5, "kKpzcc", 4)); session.close(); System.out.println(update); } */ /* @Test public void testDelete() { SqlSessionFactory factory = MybatisUtils.getFactory(); SqlSession session = factory.openSession(true); String statement = "com.kpzc.test2.userMapper.deleteUser"; int delete = session.delete(statement, 5); session.close(); System.out.println(delete); }*/ @Test public void testGetUser() { SqlSessionFactory factory = MybatisUtils.getFactory(); SqlSession session = factory.openSession(true); String statement = "com.kpzc.test2.userMapper.getUser"; User user = session.selectOne(statement, 1); session.close(); System.out.println(user); } /*@Test public void testGetAll() { SqlSessionFactory factory = MybatisUtils.getFactory(); SqlSession session = factory.openSession(true); String statement = "com.kpzc.test2.userMapper.getAllUsers"; List<User> list = session.selectList(statement); session.close(); System.out.println(list); } */ }
5.还有conf.xml(添加上这句<mapper resource="com/kpzc/test2/userMapper.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> <!-- development : 开发模式 work : 工作模式 --> <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://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="" /> </dataSource> </environment> </environments> <mappers> <!-- 写mapper所在的包的路径 --> <mapper resource="com/kpzc/test1/userMapper.xml"/> <mapper resource="com/kpzc/test2/userMapper.xml"/> </mappers> </configuration>