SpringMVC整合Mybatis

blue星空發表於2020-07-17

 

1.pom.xml中新增引入架包

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
  </dependency>
  <dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.5</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.3.5.RELEASE</version>
  </dependency>

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

  

 
2,applicationContext.xml中新增配置
<context:property-placeholder location="classpath:jdbc.properties" />


<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass" value="${jdbc.driver}" />
  <property name="jdbcUrl" value="${jdbc.url}" />
  <property name="user" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
  <!-- 當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數 -->
  <property name="acquireIncrement" value="${jdbc.pool.increment}"/>
  <!-- 初始連線池大小 -->
  <property name="initialPoolSize" value="${jdbc.pool.init}"/>
  <!-- 連線池中連線最小個數 -->
  <property name="minPoolSize" value="${jdbc.pool.min}"/>
  <!-- 連線池中連線最大個數 -->
  <property name="maxPoolSize" value="${jdbc.pool.max}"/>
</bean>


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="datasource"/>
  <!--    指定*Mapper.xml位置-->
  <property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
  <!--    指定的是介面(Java)路徑-->
  <property name="basePackage" value="com.blueStarWei.mapper" />
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>


<!-- 宣告式事務管理 : 定義事物管理器,由spring管理事務 -->
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="datasource"/>
</bean>

  

3,建立jdbc.properties【resources路徑下】

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/wei_db
jdbc.username=root
jdbc.password=root


#pool settings
jdbc.pool.init=10
jdbc.pool.increment=3
jdbc.pool.min=5
jdbc.pool.max=20

  

 
4,建立PersonMapper.java【java路徑下】
package com.blueStarWei.mapper;


import com.blueStarWei.bean.Person;


public interface PersonMapper {


    int addPerson(Person person);


    String getName(int id);


    Person getPersonById(int id);
}

  

5, 建立PersonMapper.xml【resources/mapper路徑下】

<?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.blueStarWei.mapper.PersonMapper" >


  <insert id="addPerson" >
    insert into person(id,name,age,address) values(#{id},#{name},#{age},#{address})
  </insert>


  <select id="getName" parameterType="int" resultType="java.lang.String">
    select name from person where id = #{id}
  </select>


  <select id="getPersonById" parameterType="int" resultType="com.blueStarWei.bean.Person" >
    select * from person where id = #{id}
  </select>
</mapper>

  

6,修改controller【java路徑下】

package com.blueStarWei.controller;


import com.blueStarWei.mapper.PersonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/hello")
public class HelloWorld {


    @Autowired
    private PersonMapper person;


    @RequestMapping("/name/{id}")
    public String say(@PathVariable int id){
        return person.getName(id);
    }
}

  

備註:

如果*Mapper.xml放置在src/main/java路徑下,需要在pom.xml種新增如下配置。(IDEA預設只編譯resources下的xml檔案)

<!-- 促使IDEA編譯src/main/java路徑下的xml-->
<build>
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>
  </resources>
</build>

  

相關文章