(1)pom文件导入相关依赖
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<!-- MyBatis 与 Spring 整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
实体类
package com.atgugu.crowd.empty;
public class Admin {
private Integer id;
//登录账号
private String loginAcct;
// 登录密码
private String userPswd;
// 用户名
private String userName;
// 电子邮件
private String email;
//创建时间
private String createTime;
//无参构造
public Admin() {
}
// 有参构造
public Admin(Object o, String tom, String s, String 同名, String s1, Object o1) {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLoginAcct() {
return loginAcct;
}
public void setLoginAcct(String loginAcct) {
this.loginAcct = loginAcct == null ? null : loginAcct.trim();
}
public String getUserPswd() {
return userPswd;
}
public void setUserPswd(String userPswd) {
this.userPswd = userPswd == null ? null : userPswd.trim();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime == null ? null : createTime.trim();
}
}
接口
package com.atgugu.crowd.mapper;
import com.atgugu.crowd.empty.Admin;
import com.atgugu.crowd.empty.AdminExample;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface AdminMapper {
int insert(Admin record);
Mapper文件
<insert id="insertSelective" parameterType="com.atgugu.crowd.empty.Admin" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Sep 18 18:52:00 CST 2020.
-->
insert into t_admin
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="loginAcct != null" >
login_acct,
</if>
<if test="userPswd != null" >
user_pswd,
</if>
<if test="userName != null" >
user_name,
</if>
<if test="email != null" >
email,
</if>
<if test="createTime != null" >
create_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{
id,jdbcType=INTEGER},
</if>
<if test="loginAcct != null" >
#{
loginAcct,jdbcType=VARCHAR},
</if>
<if test="userPswd != null" >
#{
userPswd,jdbcType=CHAR},
</if>
<if test="userName != null" >
#{
userName,jdbcType=VARCHAR},
</if>
<if test="email != null" >
#{
email,jdbcType=VARCHAR},
</if>
<if test="createTime != null" >
#{
createTime,jdbcType=CHAR},
</if>
</trim>
</insert>
操作清单
在子工程中加入搭建环境所需要的具体依赖 准备 jdbc.properties
创建 Spring 配置文件专门配置 Spring 和 MyBatis 整合相关 在 Spring 的配置文件中
加载 jdbc.properties 属性文件 配置数据源 测试从数据源中获取数据库连接
配置 SqlSessionFactoryBean 装配数据源
指定 XxxMapper.xml 配置文件的位置 指定 MyBatis 全局配置文件的位置(可选)
配置 MapperScannerConfigurer
测试是否可以装配 XxxMapper 接口并通过这个接口操作数据库
<?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
http://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:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 连接数据库的用户名 -->
<property name="username" value="${jdbc.user}"/>
<!-- 连接数据库的密码 -->
<property name="password" value="${jdbc.password}"/>
<!-- 目标数据库的 URL 地址 -->
<property name="url" value="${jdbc.url}"/>
<!-- 数据库驱动全类名 -->
<property name="driverClassName" value="${jdbc.driver}"/>
</bean>
<!-- 配置 SqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 装配数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 指定 MyBatis 全局配置文件位置 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<!-- 指定 Mapper 配置文件位置 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>
</bean>
<!-- 配置 MapperScannerConfigurer -->
<!-- 把 MyBatis 创建的 Mapper 接口类型的代理对象扫描到 IOC 容器中 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 使用 basePackage 属性指定 Mapper 接口所在包 -->
<property name="basePackage" value="com.atgugu.crowd.mapper"/>
</bean>
</beans>
配置这块一定要仔细,很容易出错