日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mybatis与spring结合

發布時間:2023/12/18 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis与spring结合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 使用步驟
    • 添加依賴包
    • 配置數據源
    • 配置Spring、Mybatis結合
    • 使用
    • applicationContext.xml
    • CityMapper.xml
    • 實體類
    • CityMapper.java(接口)
    • 程序運行入口

使用步驟

添加依賴包

<dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.1</version> </dependency>

配置數據源

<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結合

<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>

使用

// 經過上面的幾步配置后, 我們就可以直接從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對象已經自動注入到Service中了)MessageServiceImpl messageService = ctx.getBean(MessageServiceImpl.class);// 3. 執行查詢所有方法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、配置數據源 --><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整個的管理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>

實體類

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();} }

程序運行入口

public class App {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");System.out.println(ac.getBean(CityServiceImpl.class).queryAll());} }

總結

以上是生活随笔為你收集整理的mybatis与spring结合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。