标签:private ide java代码 build stream from 实体类 actor manage
对于上一节中的查询我们还可以通过接口的方式进行编程,开发环境和上一节一样
<?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="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/spring_mybatis" /> <property name="username" value="root" /> <property name="password" value="tuniu520" /> </dataSource> </environment> </environments> </configuration>
package com.pjf.mybatis.po; public class Hotel { private int hotelId; private String hotelName; private String hotelAddress; private int price; public int getHotelId() { return hotelId; } public void setHotelId(int hotelId) { this.hotelId = hotelId; } public String getHotelName() { return hotelName; } public void setHotelName(String hotelName) { this.hotelName = hotelName; } public String getHotelAddress() { return hotelAddress; } public void setHotelAddress(String hotelAddress) { this.hotelAddress = hotelAddress; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Hotel [hotelId=" + hotelId + ", hotelName=" + hotelName + ", hotelAddress=" + hotelAddress + ", price=" + price + "]"; } }
package com.pjf.mybatis.dao; import com.pjf.mybatis.po.Hotel; public interface HotelMapper { public Hotel getHotel(Integer i); }
这里修改了namespace,定义为接口的类全名,上一节这里可以随便写。
然后修改了sql的id,对应到接口中的方法,这样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写hotelMapper接口的类全名,和接口一一对应 --> <mapper namespace="com.pjf.mybatis.dao.HotelMapper"> <!-- 这里的id写hotelMapper接口对应的方法名 --> <select id="getHotel" resultType="com.pjf.mybatis.po.Hotel"> select id,hotel_name as hotelName, hotel_address as hotelAddress, price from hotel where id=#{id} </select> </mapper>
package com.pjf.mybatis; import java.io.IOException; import java.io.InputStream; 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 org.junit.Test; import com.pjf.mybatis.dao.HotelMapper; import com.pjf.mybatis.po.Hotel; public class TestHotel { public SqlSessionFactory sqlSessionFactory() throws IOException { // mybatis的配置文件 String resource = "mybatis_config.xml"; // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)TestHotel.class.getClassLoader() InputStream is = Resources.getResourceAsStream(resource); // 构建sqlSession的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); return sessionFactory; } @Test public void getHotel() throws IOException { //如果有多个会话,会新建多个SqlSessionFactory,将重复的内容通过函数提取出来了 SqlSessionFactory sessionFactory = sqlSessionFactory(); // 打开jdbc会话 SqlSession session = sessionFactory.openSession(); // 创建hotelMapper类的实例 // 虽然这是个接口,但是mybatis会通过动态代理新建一个实体类 // 可以通过System.out.println(hotelMapper.getClass())查看该类的类型 // 输出class com.sun.proxy.$Proxy5,可以看到是一个代理类 HotelMapper hotelMapper = session.getMapper(HotelMapper.class); System.out.println(hotelMapper.getClass()); Hotel hotel = hotelMapper.getHotel(1001); System.out.println(hotel); session.close(); } }
可以看到结果和之前一致。
标签:private ide java代码 build stream from 实体类 actor manage
原文地址:http://www.cnblogs.com/pjfmeng/p/7676653.html