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>