疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

如何在spring3中使用mybatis


 

如何在spring3中使用mybatis呢?这是很多java培训的学员都在问的问题,以下广州疯狂java就为大家来介绍一下!

1.MyBatis 例子

  首先,单独使用MyBatis时:

  [java]

  import java.io.IOException;

  import java.io.Reader;

  import org.apache.ibatis.io.Resources;

  import org.apache.ibatis.session.SqlSessionFactory;

  import org.apache.ibatis.session.SqlSessionFactoryBuilder;

  public class MyBatisUtil {

  // 每一个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心

  // 使用SqlSessionFactory的最佳实践是在应用运行期间不要重复创建多次,最佳范围是应用范围

  private final static SqlSessionFactory sqlSessionFactory;

  static {

  String resource = "configuration.xml";

  Reader reader = null;

  try {

  reader = Resources.getResourceAsReaderresource);

  } catch IOException e {

  System.out.printlne.getMessage());

  }

  // SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象来获得

  // SqlSessionFactoryBuilder实例的最佳范围是方法范围(也就是本地方法变量)。

  sqlSessionFactory = new SqlSessionFactoryBuilder()。buildreader);

  }

  public static SqlSessionFactory getSqlSessionFactory() {

  return sqlSessionFactory;

  }

  }

配置文件:

  [html]

  <?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="Mapper.UserMapper">

  <!-- 这里namespace必须是UserMapper接口的路径,不然要运行的时候要报错 "is not known to the MapperRegistry"-->

  <insert id="insertUser" parameterType="User">

  insert into vincent_username,age values#{name},#{age}

  <!-- 这里sql结尾不能加分号,否则报"ORA-00911"的错误 -->

  </insert>

  <!-- 这里的id必须和UserMapper接口中的接口方法名相同,不然运行的时候也要报错 -->

  <select id="getUser" resultType="User" parameterType="java.lang.String">

  select * from vincent_user where name=#{name}

  </select>

  </mapper>

  使用的测试类为:

  [java]

  import org.apache.ibatis.session.SqlSession;

  import org.apache.ibatis.session.SqlSessionFactory;

  import org.junit.Test;

  public class TestMapper {

  static SqlSessionFactory sqlSessionFactory = null;

  static {

  sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();

  }

  @Test

  public void testAdd() {

  SqlSession sqlSession = sqlSessionFactory.openSession();

  try {

  UserMapper userMapper = sqlSession.getMapperUserMapper.class);

  User user = new User"tom",new Integer5));

  userMapper.insertUseruser);

  sqlSession.commit();//这里一定要提交,不然数据进不去数据库中

  } finally {

  sqlSession.close();

  }

  }

  @Test

  public void getUser() {

  SqlSession sqlSession = sqlSessionFactory.openSession();

  try {

  UserMapper userMapper = sqlSession.getMapperUserMapper.class);

  User user = userMapper.getUser"jun");

  System.out.println"name: "+user.getName()+"|age: "+user.getAge());

  } finally {

  sqlSession.close();

  }

  }

  }

  2.整合Spring3后,单独使用Mybatis

  首先,Spring3配置文件中(applicationContext.xml)有:

  [html]

  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

  <property name="configLocation" value="classpath:ibatis-config.xml" />

  <property name="dataSource" ref="dataSource" />

  <!-- mapperresultmap配置路径 -->

  <property name="mapperLocations">

  <span style="white-space:pre">  </span><list>

  <span style="white-space:pre">  </span><value>classpath:com/log/bean/mapper/*.xml</value>

  <span style="white-space:pre">  </span></list>

  </property>

  </bean>

  [html]

  <!-- 通过扫描的模式,扫描目录在com/log/bean/mapper目录下,所有的mapper都继承

  SQLMapper接口的接口, 这样一个bean就可以了 -->

  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

  <property name="basePackage" value="com.log.bean.mapper"/>

  <property name="markerInterface" value="com.log.bean.mapper.SQLMapper"/>

  </bean>

  工具类:

  [java]

  import org.apache.ibatis.session.SqlSessionFactory;

  import org.springframework.context.ApplicationContext;

  import org.springframework.context.support.FileSystemXmlApplicationContext;

  public class MyBatisUtil  {

  private  final static SqlSessionFactory sqlSessionFactory;

  static {

  ApplicationContext ac = new FileSystemXmlApplicationContext"classpath:applicationContext.xml");

  sqlSessionFactory = SqlSessionFactoryac.getBean"sqlSessionFactory");

  }

  public static SqlSessionFactory getSqlSessionFactory() {

  return sqlSessionFactory;

  }

  }

以上介绍的就是如何在spring3中使用mybatis的知识了,希望能对大家有帮助!现在java行业风生水起,学习java正好赶上了,那么该如何学习java呢?疯狂java的老师建议最好去正规的java培训机构参加java培训,只有这样才能学到真正的知识!