mybatis进阶
1.mybatis一对一映射
Student--Card
<?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.juaner.one2one.Student">
<resultMap id="studentMap" type="com.juaner.one2one.Student">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<!--引入CardMapper.xml文件中的映射
property:Student类的关联属性
resultMap:引入CardMapper.xml中的映射类型 namespace+id
-->
<association property="card" resultMap="com.juaner.one2one.Card.cardMap"/>
</resultMap>
<select id="findById" parameterType="int" resultMap="studentMap">
SELECT s.sid,s.sname,c.cid,c.cnum
FROM student s,card c
WHERE s.scid = c.cid
AND c.cid =#{id}
</select>
<!--只封装查询出来的字段-->
<select id="findByName" parameterType="string" resultMap="studentMap">
SELECT s.sid,c.cnum
FROM student s,card c
WHERE s.scid = c.cid
AND s.sname = #{name}
</select>
</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">
<mapper namespace="com.juaner.one2one.Card">
<resultMap id="cardMap" type="com.juaner.one2one.Card">
<id property="id" column="cid"/>
<result property="num" column="cnum"/>
</resultMap>
</mapper>
2.mybatis一对多映射
Student---Grade
<?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.juaner.one2many.Student">
<resultMap id="studentMap" type="com.juaner.one2many.Student">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="grade" resultMap="com.juaner.one2many.Grade.gradeMap"/>
</resultMap>
<select id="findAllByName" parameterType="string" resultMap="studentMap">
SELECT s.sid,s.sname,g.gid,g.gname
FROM studentg s,grade g
WHERE s.sgid = g.gid
AND g.gname=#{name}
</select>
</mapper>
<mapper namespace="com.juaner.one2many.Grade">
<resultMap id="gradeMap" type="com.juaner.one2many.Grade">
<id property="id" column="gid"/>
<result property="name" column="gname"/>
</resultMap>
<select id="findByName" parameterType="string" resultMap="gradeMap">
SELECT s.sid,s.sname,g.gid,g.gname
FROM studentg s,grade g
WHERE s.sgid = g.gid
AND s.sname = #{name}
</select>
</mapper>
3.mybatis多对多映射
Student--Course
<?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.juaner.many2many.Student">
<resultMap id="studentMap" type="com.juaner.many2many.Student">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
</resultMap>
<select id="findAllByCourseName" parameterType="string" resultMap="studentMap">
SELECT s.sid,s.sname,c.cid,c.cname
FROM studentc s,courses c,middles m
WHERE s.sid = m.msid AND c.cid=m.mcid
AND c.cname=#{name}
</select>
</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">
<mapper namespace="com.juaner.many2many.Course">
<resultMap id="courseMap" type="com.juaner.many2many.Course">
<id property="id" column="cid"/>
<result property="name" column="cname"/>
</resultMap>
<select id="findAllByName" parameterType="string" resultMap="courseMap">
SELECT s.sid,s.sname,c.cid,c.cname
FROM studentc s,courses c,middles m
WHERE s.sid = m.msid AND c.cid=m.mcid
AND s.sname = #{name}
</select>
</mapper>
4.spring + mybatis + oracle开发
1)创建一个spring-mybaits-oracle javaweb工程
2)导入spring,mybatis,c3p0,oracle和mybatis提供的与spring整合的插件包
mysql的jar:
mysql-connector-java-5.1.7-bin.jar
oracle的jar:
ojdbc5.jar
c3p0的jar:
c3p0-0.9.1.2.jar
mybatis的jar:
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
mybatis-3.1.1.jar
mybatis与spring整合的jar
【mybatis-spring-1.1.1.jar】
spring的ioc模块的jar:
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
commons-logging.jar
spring的aop模块的jar:
aopalliance.jar
aspectjweaver.jar
cglib-2.2.2.jar
org.springframework.aop-3.0.5.RELEASE.jar
spring的transaction模块的jar:
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
3) 创建emps数据库表
4)创建Emp.java类
5)创建EmpMapper.xml映射文件
6)创建mybatis.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>
<mappers>
<mapper resource="com/juaner/entity/EmpMapper.xml"/>
</mappers>
</configuration>
7)创建spring.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--配置c3p0连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="user" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!--加载mybatis配置文件和映射文件,即替代原来的mybatis工具类作用-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean> <!--配置mybatis事务管理器,因为mybatis底层用的是jdbc事务管理器
所以在这里配置jdbc事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务通知,即哪些方法需要事务支持-->
<tx:advice id="tx" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice> <!--配置事务切面,即哪些包下的类需要事务支持-->
<aop:config>
<!--返回值不限 dao包下的所有类的所有方法,参数不限-->
<aop:pointcut id="pt" expression="execution(* com.juaner.dao.*.*(..))"/>
<!--将事务切面和事务通知结合在一起-->
<aop:advisor advice-ref="tx" pointcut-ref="pt"/>
</aop:config> <!--注册empdao-->
<bean id="empDao" class="com.juaner.dao.EmpDao">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
</bean>
</beans>
8)创建EmpDao.java类
public class EmpDao {
//自动注入
private SqlSessionFactory sqlSessionFactory;
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public void add(Emp emp)throws Exception{
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert(Emp.class.getName()+".add",emp);
// int i = 1/0;
sqlSession.close();
}
}
9)测试
public class TestEmpDao {
@Test
public void test()throws Exception{
EmpDao empDao = new EmpDao();
empDao.add(new Emp(1,"m",6000d,"小明"));
}
//测试spring整合mybatis
@Test
public void test2()throws Exception{
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});
EmpDao empDao = (EmpDao) ac.getBean("empDao");
empDao.add(new Emp(2,"f",7000d,"小红"));
}
}
mybatis进阶的更多相关文章
- mybatis进阶案例之多表查询
mybatis进阶案例之多表查询 一.mybatis中表之间的关系 在数据库中,实体型之间的关系主要有如下几种: 1.一对一 如果对于实体集A中的每一个实体,实体集B中至多有一个(也可以没有)实体与之 ...
- 《Mybatis进阶》肝了30天专栏文章,整理成册,免费获取!!!
持续原创输出,点击上方蓝字关注我吧 目录 前言 简介 如何获取? 总结 前言 Mybatis专栏文章写到至今已经有一个月了,从基础到源码详细的介绍了每个知识点,没什么多余的废话,全是工作.面试中常用到 ...
- mybatis进阶--一对一查询
所谓的一对一查询,就是说我们在查询一个表的数据的时候,需要关联查询其他表的数据. 需求 首先说一个使用一对一查询的小需求吧:假设我们在查询某一个订单的信息的时候,需要关联查询出创建这个订单对应的用户信 ...
- MyBatis进阶(一)运行原理
初次学习MyBatis,自己花了不少时间,理解一件事物是需要时间的.经过多次反复的理解,你的认知能力就可以得到提升.以下是学习MyBatis的一些理解认识,技术理解上若有不当之处,敬请朋友们提出宝贵意 ...
- MyBatis进阶使用——动态SQL
MyBatis的强大特性之一就是它的动态SQL.如果你有使用JDBC或者其他类似框架的经验,你一定会体会到根据不同条件拼接SQL语句的痛苦.然而利用动态SQL这一特性可以彻底摆脱这一痛苦 MyBati ...
- MyBatis进阶(三)
MyBatis批量新增数据 1. 传统的JDBC批量插入数据 使用for循环 创建连接 获取连接 创建sql语句,交给连接 使用for循环新增数据 提交连接 使用批处理 两者都存在严重的效率问题,代码 ...
- MyBatis进阶(二)
MyBatis之动态SQL 动态SQL之foreach 有时SQL语句where条件是在一个集合或者数组里,需要使用in关键字,这时可以使用foreach动态SQL语句,例如: select * fr ...
- MyBatis进阶(一)
MyBatis参数传递 1. MyBatis单参数传递 单参数传递不做特殊处理,直接取出参数值赋给xml文件,如#{id} 2. MyBatis多参数传递 多参数传递默认使用{arg1, arg0, ...
- mybatis进阶--一对多查询
首先,我们还是先给出一个需求:根据订单id查询订单明细——我们知道,一个订单里面可以有多个订单的明细(需求不明确的同学,请留言或者去淘宝网上的订单处点一下就知道了).这个时候,一个订单,对应多个订单的 ...
随机推荐
- ELK修炼之道
看了ELK大半年了,现在就慢慢的总结一下对ELK的理解 参考资料 ELK stack中文指南 Elasticsearch权威指南 官方文档 Elasticsearch基础篇 此篇用于介绍Elastic ...
- Linux VFS中write系统调用实现原理【转】
转自:http://blog.chinaunix.net/uid-28362602-id-3425881.html 目录 用户空间的write函数在内核里面的服务例程为sys_write Vfs_wr ...
- CentOS配置SSH免密码登录后,仍提示输入密码
CentOS配置SSH无密码登录需要3步: 生成公钥和私钥 导入公钥到认证文件,更改权限 测试 1.生成公钥和私钥 ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa 默 ...
- Hadoop之hive的drop table恢复
一.引言: 快下班的时候我开发同事问能不能将hive中drop掉的数据恢复过来,我记得是有开回收站的,当时我回答说可以恢复的. 二.恢复过程: 在之前我有对hadoop的回收站有过了解,就是将hdfs ...
- Map.Entry
Map.Entry Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法,keySet()方法返回值是Map中 ...
- 【转】Struts1.x系列教程(7):Logic标签库
转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/0 ...
- python-判断系统平台
1.windows 2.linux 总结 python提供了sys,os及platform等个模块读取平台信息,客官可以根据自己的喜好选择使用
- GMF:如何让网格显示在background,而不是foreground
前言 很久没写文章了,准备写一系列关于Eclipse RCP /Plugin的文章. 这些文章都是trouble shooting性质的,不准备写的很细,当你碰到这样的问题,google到时,能帮你把 ...
- 【Spring】Junit加载Spring容器作单元测试
如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单元测试. > 基本的搭建 (1)引入所需的包 & ...
- [问题2014S07] 解答
[问题2014S07] 解答 (本解答由沈启帆同学提供) 由复旦高代教材 P265 引理 7.4.1 知 \(F(P_i(\lambda)^{e_i})\) 的不变因子组为 \[1,\cdots, ...