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,"小红")); } }