(1)将mybatis-3.2.0.jar,mybatis-spring-1.2.0.jar添加到web-inf/lib下,修改spring.xml,配置数据源及mybatis的SqlSessionFactoryBean。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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="CT"/>
<property name="jndiEnvironment">
<props>
<!-- The value of Context.PROVIDER_URL -->
<prop key="java.naming.provider.url">t3://localhost:9001</prop>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
</props>
</property>
</bean>
<!-- 配置mybitas SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="/WEB-INF/sqlMapConfig.xml"></property>
</bean>
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.xy6"></context:component-scan>
<bean id="demoAction" class="com.xy6.DemoAction" />
</beans>
(2)在web-inf根目录下新建文件sqlMapConfig.xml,配置mybatis相关参数。sqlMapConfig.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>
<typeAliases>
<typeAlias alias="demoVO" type="com.xy6.DemoVO"/>
</typeAliases>
<mappers>
<mapper resource="com/xy6/demo.xml"/>
</mappers>
</configuration>
(3)在com.xy6包下新建mybatissql配置文件demo.xml,配置操作数据库的各种sql语句。demo.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">
<mapper namespace="demo">
<resultMap id="demoVO" type="demoVO">
<result property="code" column="htlx" />
<result property="name" column="htlxmc" />
</resultMap>
<select id="selectByCode" resultMap="demoVO">
select htlx, htlxmc from dmrefjj.tm_r_jj_htlx
where htlx = #{code}
</select>
</mapper>
(4)在com.xy6下新建VO类DemoVO.java,对应sql查询出的数据表字段。DemoVO.java代码:
package com.xy6;
public class DemoVO {
private static final long serialVersionUID = 1L;
private int code;
private String name;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
String strTemp = "";
strTemp += "code:";
strTemp += this.getCode();
strTemp += "\n";
strTemp += "name:";
strTemp += this.getName();
strTemp += "\n";
return strTemp;
}
}
(5)在com.xy6包下新建DemoDao类DemoDao.java,调用mybatis操作数据库的接口实现操作数据库。DemoDao.java代码:
package com.xy6;
import java.util.List;
import org.mybatis.spring.support.SqlSessionDaoSupport;
public class DemoDao extends SqlSessionDaoSupport {
public List<DemoVO> selectByCode(DemoVO demoVO){
return getSqlSession().selectList("selectByCode", demoVO);
}
}
(6)修改DemoAction.java,调用DemoDao中的selectByCode方法实现读取数据库。DemoAction.java代码:
package com.xy6;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
public class DemoAction extends ActionSupport {
public static final long serialVersionUID = 1L;
@Autowired
private DemoDao demoDao;
public String execute(){
DemoHelper demoHelper = new DemoHelper();
System.out.println("---curren year:"+demoHelper.getCurYear());
DemoVO demoVO = new DemoVO();
demoVO.setCode(1);
List<DemoVO> listDemoVO = demoDao.selectByCode(demoVO);
for(int i=0;i <listDemoVO.size(); i++){
System.out.println("---i:"+listDemoVO.get(i).toString());
}
return "success";
}
}
(7)修改spring.xml,注册DemoDao实体bean。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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="CT"/>
<property name="jndiEnvironment">
<props>
<!-- The value of Context.PROVIDER_URL -->
<prop key="java.naming.provider.url">t3://localhost:9001</prop>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
</props>
</property>
</bean>
<!-- 配置mybitas SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="/WEB-INF/sqlMapConfig.xml"></property>
</bean>
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.xy6"></context:component-scan>
<bean id="demoAction" class="com.xy6.DemoAction" />
<bean id="demoDao" class="com.xy6.DemoDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
(8)登录weblogic控制台,配置jndiName为CT的数据源。如下图:
(9)重启weblogic域服务,浏览器中访问http://localhost:9001/web/demo.action,页面显示demo.jsp,控制台输出2014及查询出的数据。如下图:
(10)至此,mybatis配置结束。