mybatis与spring结合
生活随笔
收集整理的這篇文章主要介紹了
mybatis与spring结合
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 使用步驟
- 添加依賴包
- 配置數(shù)據(jù)源
- 配置Spring、Mybatis結(jié)合
- 使用
- applicationContext.xml
- CityMapper.xml
- 實(shí)體類
- CityMapper.java(接口)
- 程序運(yùn)行入口
使用步驟
添加依賴包
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.1</version> </dependency>配置數(shù)據(jù)源
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="url" value="${jdbc.url}"/><property name="driverClassName" value="${jdbc.driver}"/><property name="username" value="${jdbc.user}"/><property name="password" value="${jdbc.password}"/> </bean>配置Spring、Mybatis結(jié)合
<bean class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><!-- 指定XML Mapper文件的路徑 --><property name="mapperLocations" value="classpath:/mapper/*"/> </bean> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://mybatis.org/schema/mybatis-springhttp://mybatis.org/schema/mybatis-spring.xsd"><!-- 自動掃描所有Mapper接口 --><mybatis:scan base-package="com.lanou3g.spring.mapper" /> </beans>使用
// 經(jīng)過上面的幾步配置后, 我們就可以直接從Spring的IOC容器中獲取Mapper操作接口了public class Application {public static void main(String[] args) throws SQLException {// 1. 初始化IOC容器ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");// 2. 從容器中獲取Service對象(Mapper對象已經(jīng)自動注入到Service中了)MessageServiceImpl messageService = ctx.getBean(MessageServiceImpl.class);// 3. 執(zhí)行查詢所有方法List<Message> messageList = messageService.queryAll();log.info("" + messageList);} }applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://mybatis.org/schema/mybatis-springhttp://mybatis.org/schema/mybatis-spring.xsd"><!-- 1、開啟注解掃描 --><context:component-scan base-package="com.lanou3g.spring"/><!-- 2、配置數(shù)據(jù)源 --><context:property-placeholder location="classpath:jdbc.properties" /><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="url" value="${jdbc.url}"/><property name="driverClassName" value="${jdbc.driver}"/><property name="username" value="${jdbc.user}"/><property name="password" value="${jdbc.password}"/></bean><!-- 3、配置Spring、Mybatis整個(gè)的管理Bean --><bean class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="mapperLocations" value="classpath:/mapper/*"/></bean><!-- 4、自動掃描所有Mapper接口 --><mybatis:scan base-package="com.lanou3g.spring.mapper" /> </beans>CityMapper.xml
用genarator工具自動生成的
<?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.lanou3g.spring.mapper.CityMapper"><resultMap id="BaseResultMap" type="com.lanou3g.spring.bean.City"><id column="id" jdbcType="INTEGER" property="id" /><result column="cname" jdbcType="VARCHAR" property="cname" /><result column="pid" jdbcType="INTEGER" property="pid" /></resultMap><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">delete from citywhere id = #{id,jdbcType=INTEGER}</delete><insert id="insert" parameterType="com.lanou3g.spring.bean.City"><selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">SELECT LAST_INSERT_ID()</selectKey>insert into city (cname, pid)values (#{cname,jdbcType=VARCHAR}, #{pid,jdbcType=INTEGER})</insert><update id="updateByPrimaryKey" parameterType="com.lanou3g.spring.bean.City">update cityset cname = #{cname,jdbcType=VARCHAR},pid = #{pid,jdbcType=INTEGER}where id = #{id,jdbcType=INTEGER}</update><select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">select id, cname, pidfrom citywhere id = #{id,jdbcType=INTEGER}</select><select id="selectAll" resultMap="BaseResultMap">select id, cname, pidfrom city</select> </mapper>實(shí)體類
public class City {private Integer id;private String cname;private Integer pid;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname == null ? null : cname.trim();}public Integer getPid() {return pid;}public void setPid(Integer pid) {this.pid = pid;}@Overridepublic String toString() {return "City{" +"id=" + id +", cname='" + cname + '\'' +", pid=" + pid +"}\n";} }CityMapper.java(接口)
public interface CityMapper {int deleteByPrimaryKey(Integer id);int insert(City record);City selectByPrimaryKey(Integer id);List<City> selectAll();int updateByPrimaryKey(City record); }CityServiceImpl.java(service層)
@Service public class CityServiceImpl {@Autowiredprivate CityMapper cityMapper;public List<City> queryAll(){return cityMapper.selectAll();} }程序運(yùn)行入口
public class App {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");System.out.println(ac.getBean(CityServiceImpl.class).queryAll());} }總結(jié)
以上是生活随笔為你收集整理的mybatis与spring结合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P3369 【模板】普通平衡树(fhq
- 下一篇: html元素span,[转载]HTML元