一、Maven依赖配置(省略版本号)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
</dependencies>
二、Spring bean 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!--业务类Bean-->
<bean id="GoodsExport" class="tg.export.GoodsExport">
<property name="id" value="5" />
<property name="goodsMapper" ref="goodsMapper"/>
</bean>
<!--数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql://127.0.0.1/test?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!--实例化数据库对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/Configuration.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!--DAO层接口-->
<bean id="goodsMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="tg.mapper.GoodsMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
三、数据库配置文件———Configuration.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="config/sqlMap/GoodsMapper.xml" />
</mappers>
</configuration>
三、数据库Mapper配置文件———GoodsMapper.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="tg.mapper.GoodsMapper">
<select id="getName" parameterType="Integer" resultType="String">
SELECT `name` from t_wm_tg_goods where id=#{id}
</select>
</mapper>
四、SpringBean
public class GoodsExport {
private Integer id;
private GoodsMapper goodsMapper;
public Integer addGoods() {
return this.id;
}
public String getGoodName(){
return goodsMapper.getName(this.id);
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setGoodsMapper(GoodsMapper goodsMapper) {
this.goodsMapper = goodsMapper;
}
public GoodsMapper getGoodsMapper() {
return goodsMapper;
}
}
五、执行代码
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"config/applicationContext.xml");
GoodsExport obj = (GoodsExport) context.getBean("GoodsExport");
System.out.println(obj.getGoodName());
}
}
六、结果
huangyian7