1、Maven 依赖
<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>
<parent>
<groupId>constxiong</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>mybatis-spring</artifactId>
<dependencies>
<!-- MySQL 8.0.21 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!-- MyBatis-Spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!-- spring context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
</dependencies>
<build>
</build>
</project>
2、Spring 配置文件
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
<context:property-placeholder location="classpath:db.properties" ignore-unresolvable="true" />
<bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
<!-- 基本属性 url、user、password -->
<property name="driver" value="${jdbc_driver}" />
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_username}"/>
<property name="password" value="${jdbc_password}"/>
</bean>
<!-- spring 和 Mybatis整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:constxiong/mapper/*.xml" />
</bean>
<!-- DAO接口所在包,配置自动扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="constxiong.mapper"/>
</bean>
</beans>
3、db配置
#5.0 5.7
#jdbc_driver=com.mysql.jdbc.Driver
#jdbc_url=jdbc\:mysql\://localhost\:3306/blog?useUnicode\=true&allowMultiQueries\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull
#jdbc_username=root
#jdbc_password=root
#8.0.21
jdbc_driver=com.mysql.cj.jdbc.Driver
jdbc_url=jdbc\:mysql\://localhost\:3306/constxiong?serverTimezone\=UTC&useSSL\=false&useUnicode\=true&characterEncoding\=UTF-8
jdbc_username=root
jdbc_password=constxiong@123
3、Mapper 接口
package constxiong.mapper;
import constxiong.po.User;
import java.util.List;
/**
* UserMapper 接口,映射对数据库的操作
*/
public interface UserMapper {
List<User> selectUsers();
int insertUser(User user);
int deleteUsers();
}
4、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="constxiong.mapper.UserMapper">
<select id="selectUsers" resultType="constxiong.po.User">
select * from user
</select>
<insert id="insertUser" parameterType="constxiong.po.User">
insert into user values(#{id}, #{name})
</insert>
<delete id="deleteUsers">
delete from user
</delete>
</mapper>
5、User PO
package constxiong.po;
/**
* 用户表模型
*/
public class User {
private Integer id;
private String name;
private String mc;
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMc() {
return mc;
}
public void setMc(String mc) {
this.mc = mc;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", mc='" + mc + '\'' +
'}';
}
}
6、测试代码
ConstXiong 备案号:苏ICP备16009629号-3