之前学习了spring和springmvc的整合,今天我们来学习一下spring和mybatis的整合
我得开发环境还是eclipse 2018-12 jdk1.8 tomcat9.0 mybatis3 spring5
具体步骤如下:
首先创建一个maven工程,具体的创建方法见上一篇文章
不过这次我们打的是jar包(不需要web模块),引入spring的一些依赖包,mybatis的依赖,spring和mybatis的连接依赖,java的mysql的驱动包,c3p0连接池
我得pom文如下
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.miracle</groupId>
<artifactId>sm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
然后将其中的包进行扫描
<context:component-scan base-package="com"></context:component-scan>
然后在其中配置数据源,数据源的配置利用的是com.mchange.v2.c3p0.ComboPooledDataSource的连接池(C3P0连接池)
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
我将具体的值写在了jdbc.properties中,具体文件如下
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
jdbc.username=root
jdbc.password=root
再将其引入
<context:property-placeholder
location="classpath:jdbc.properties" />
再利用spring为我们提供的org.mybatis.spring.SqlSessionFactoryBean来创建sqlSessionFactory
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations"
value="classpath:mapper/*.xml"></property>
</bean>
然后配置mapper文件所在位置
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"></property>
</bean>
然后写pojo类Login
package com.pojo;
public class Login {
private int id;
private String username;
private String password;
public Login() {
super();
// TODO Auto-generated constructor stub
}
public Login(String username, String password) {
super();
this.username = username;
this.password = password;
}
public Login(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
然后就是mapper接口了LoginMapper
package com.dao;
import org.springframework.stereotype.Repository;
import com.pojo.Login;
@Repository
public interface LoginMapper {
Login findLoginById(int id);
}
最后写mapper的实现配置文件LoginMapper.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="com.dao.LoginMapper">
<select id="findLoginById" parameterType="java.lang.Integer" resultType="com.pojo.Login">
select * from login where id = #{id}
</select>
</mapper>
最后我们创建一个Test的类来测试一下刚才所写的查询
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dao.LoginMapper;
import com.pojo.Login;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
LoginMapper loginMapper = (LoginMapper) ac.getBean("loginMapper");
Login login = loginMapper.findLoginById(1);
System.out.println(login.getUsername());
}
}
image.png
输出结果123,大功告成!!!
网友评论