步骤:
- 1、配置mybatis:对象、关系、映射(namesapce=DAO接口(包名+类名),id=DAO的方法名称)、DAO接口(@Mapper)
- 2、配置spring-contenxt.xml
- 3、测试执行
Student.java
public class Student {
private Integer sId;
private String sName;
public Integer getSId() {
return sId;
}
public void setSId(Integer sId) {
this.sId = sId;
}
public String getSName() {
return sName;
}
public void setSName(String sName) {
this.sName = sName;
}
@Override
public String toString() {
return "Student{" +
"sId=" + sId +
", sName='" + sName + '\'' +
'}';
}
}
StudentDao.java
@Mapper
public interface StudentDao {
Student findStudentById(Integer id);
}
Student.mapper.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.jx.dao.StudentDao">
<resultMap id="baseMap" type="com.jx.bean.Student">
<id property="sId" column="s_id"/>
<result property="sName" column="s_name"/>
</resultMap>
<select id="findStudentById" parameterType="int" resultMap="baseMap">
select * from student where s_id = #{sId}
</select>
</mapper>
mybatis.cfg.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
spring-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--包扫描-->
<context:component-scan base-package="com.jx"/>
<!-- 启用注解驱动-->
<mvc:annotation-driven/>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/day93"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置sqlSession-->
<bean id="sqlSessionFaction" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="mapper/*.mapper.xml"/>
<property name="configLocation" value="mybatis.cfg.xml"/>
</bean>
<!-- Mapper注解的扫描-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jx.dao"/>
</bean>
</beans>
StudentDaoTest.java
public class StudentDaoTest {
@Test
public void findStudentById(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
StudentDao studentDao = applicationContext.getBean(StudentDao.class);
Student studentById = studentDao.findStudentById(1);
System.out.println(studentById);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.jx</groupId>
<artifactId>day99_a</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>day99_a Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
</dependencies>
</project>
运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4da17f90a90af2e1095494975dd4b64a.png)
项目结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/805ce942f3ab6d5bfa7c1fa5351b77ad.png)