<! - 查询订单关联用户信息使用resultmap - > < resultMap type = “Orders” id = “orderUserResultMap” > < id column = “id” property = “id” /> < result column = “user_id” property = “userId” /> < result column = “number” property = “number” /> < result column = “createtime” property = “createtime” /> < result column = “note” property = “note” /> <! - 一对一关联映射 - > <! - 属性:订单对象的用户属性 javaType:用户属性对应的类型 - > < association property = “user” javaType = “cn.itcast.po.User” > <! - column:user表的主键对应的列属性:user对象中的id属性 - > < id column = “user_id” property = “id” /> < result column = “username” property = “username” /> < 结果列= “地址”属性= “地址” /> </ association > </ resultMap > <select id = “findOrdersWithUserResultMap” resultMap = “ orderUserResultMap ” > 选择 o.id, o.user_id, o.number, o.createtime, o.note, u.username, u.address 从 订单o 在u.id = o.user_id上加入`用户' </选择> |
< resultMap type = “user” id = “userOrderResultMap” > <! - 用户信息映射 - > < id property = “id” column = “id” /> < result property = “username” column = “username” /> < result property = “birthday” column = “birthday” /> < result property = “sex” column = “sex” /> < result property = “address” column = “address” /> <! - 一对多关联映射 - > < collection property = “orders” ofType = “orders” > < id property = “id” column = “oid” /> <! - 用户ID已经在用户对象中存在,此处可以不设置 - > <! - <result property =“userId”column =“id”/> - > < result property = “number” column = “number” /> < result property = “createtime” column = “createtime” /> < result property = “note” column = “note” /> </ collection > </ resultMap > < select id = “getUserOrderList” resultMap = “userOrderResultMap” > 选择 u。*,o.id oid, o.number, o.createtime, o.note 从 `用户`你 LEFT JOIN订单o开启u.id = o.user_id </ select > |
@测试 public void getUserOrderList (){ SqlSession session = sqlSessionFactory .openSession(); UserMapper userMapper = 会话.getMapper(UserMapper。类); 列表<User> result = userMapper .getUserOrderList(); for(User user :result ){ 系统。out .println(user ); } session .close(); } |
<?xml version = “1.0” encoding = “UTF-8”?> <!DOCTYPE 配置 PUBLIC “ - // mybatis.org //DTD Config 3.0 // EN” < 配置> < typeAliases > < package name = “cn.itcast.mybatis.pojo” /> </ typeAliases > < mappers > < mapper resource = “sqlmap / User.xml” /> </ mappers > </ configuration > |
<?xml version = “1.0” encoding = “UTF-8”?> < beans xmlns = “http://www.springframework.org/schema/beans” xmlns:context = “http://www.springframework.org/schema/context” xmlns:p = “http://www.springframework.org/schema/p” xmlns:aop = “http://www.springframework.org/schema/aop” xmlns:tx = “http://www.springframework.org/schema/tx” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http:/ /www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd “ > <! - 加载配置文件 - > < context:property-placeholder location = “classpath:db.properties” /> <! - 数据库连接池 - > < bean id = “dataSource” class = “org.apache.commons.dbcp.BasicDataSource” destroy-method = “close” > < property name = “driverClassName” value = “$ {jdbc.driver}” /> < property name = “url” value = “$ {jdbc.url}” /> < property name = “username” value = “$ {jdbc.username}” /> < property name = “password” value = “$ {jdbc.password}” /> < property name = “maxActive” value = “10” /> < property name = “maxIdle” value = “5” /> </ bean > <! - mapper配置 - > <! -让弹簧管理SqlSessionFactory中使用的MyBatis和弹簧整合包中的- > < bean id = “sqlSessionFactory” class = “org.mybatis.spring.SqlSessionFactoryBean” > <! - 数据库连接池 - > < property name = “dataSource” ref = “dataSource” /> <! -加载的MyBatis的全局配置文件- > < property name = “configLocation” value = “classpath:mybatis / SqlMapConfig.xml” /> </ bean > </ beans > |
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql:// localhost:3306 / mybatis?characterEncoding = utf-8 jdbc.username = root jdbc.password = root |
公共类 UserDaoImpl 扩展 SqlSessionDaoSupport 实现 UserDao { @覆盖 公共用户findUserById(int id)抛出异常{ SqlSession session = getSqlSession(); User user = session.selectOne(“test.findUserById” ,id); //不能关闭的SqlSession,让弹簧容器来完成 //session.close(); 返回用户; } @覆盖 公共无效 insertUser(用户用户)抛出异常{ SqlSession session = getSqlSession(); session.insert(“test.insertUser” ,用户); session.commit(); //session.close(); } } |
<! - 配置UserDao实现类 - > < bean id = “userDao” class = “cn.itcast.dao.UserDaoImpl” > < property name = “sqlSessionFactory” ref = “sqlSessionFactory” /> </ bean > |
@测试 公共无效 testFindUserById()抛出异常{ UserDao userDao =(UserDao)applicationContext .getBean(“userDao” ); User user = userDao.findUserById(1); 系统。out .println(user); } |
<! - 配置mapper代理对象 - > < bean class = “org.mybatis.spring.mapper.MapperFactoryBean” > < property name = “mapperInterface” value = “cn.itcast.mybatis.mapper.UserMapper” /> < property name = “sqlSessionFactory” ref = “sqlSessionFactory” > </ property > </ bean > |
公共类 UserMapperTest { 私人 ApplicationContext applicationContext ; @之前 公共无效 setUp()抛出异常{ applicationContext = new ClassPathXmlApplicationContext(“classpath:spring / applicationContext.xml” ); } @测试 public void testGetUserById(){ UserMapper userMapper = 的applicationContext .getBean(UserMapper。类); User user = userMapper .getUserById(1); 系统。out .println(user ); } } |
<! - 使用扫描包的形式来创建mapper代理对象 - > < bean class = “org.mybatis.spring.mapper.MapperScannerConfigurer” > < property name = “basePackage” value = “cn.itcast.mybatis.mapper” > </ property > </ bean > |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |