写在前面
很简单,大家一起快乐学习吧。
一、导入jar包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
spring的jar包就不多说了。
二、数据源
jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/student_analysis_platform
name=root
password=123456
以前是咱们mybatis-config.xml文件配置
<properties resource="jdbc.properties">
<property name="username" value="dev_user"/>
<property name="password" value="F2Fa3!33TYyg"/>
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
现在咱们spring配置数据源,通过spring的xml文件
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="password" value="${password}"/>
<property name="username" value="${name}"/>
</bean>
二、SqlSessionFactory工厂
原本:
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
现在:
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
三、mapper映射
对应表的对应实体类的Mapper接口和Mapper.xml文件是和以前一样的,
例如:
userMapper接口
package com.zhou.mapper;
import com.zhou.pojo.Depart;
import com.zhou.pojo.Role;
import com.zhou.pojo.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated Mon Aug 30 17:45:39 CST 2021
*/
int deleteByPrimaryKey(Integer userid);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated Mon Aug 30 17:45:39 CST 2021
*/
int insert(User record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated Mon Aug 30 17:45:39 CST 2021
*/
int insertSelective(User record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated Mon Aug 30 17:45:39 CST 2021
*/
User selectByPrimaryKey(Integer userid);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated Mon Aug 30 17:45:39 CST 2021
*/
int updateByPrimaryKeySelective(User record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbggenerated Mon Aug 30 17:45:39 CST 2021
*/
int updateByPrimaryKey(User record);
List<User> selectByRoleId(int roleId);
List<Role> role();
List<Depart> depart();
}
对应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.zhou.mapper.UserMapper" >
<resultMap id="BaseResultMap" type="com.zhou.pojo.User" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Aug 30 17:45:39 CST 2021.
-->
<id column="userId" property="userid" jdbcType="INTEGER" />
<result column="userName" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="roleId" property="roleid" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Aug 30 17:45:39 CST 2021.
-->
userId, userName, password, roleId
</sql>
<select id="depart" resultType="com.zhou.pojo.Depart">
SELECT
(SELECT department.departmentName FROM department WHERE department.departmentId=teacher_massage.departmentId )departmentName,
COUNT(departmentId)departmentNum FROM teacher_massage GROUP BY departmentId
</select>
<select id="role" resultType="com.zhou.pojo.Role">
select*from role
</select>
<select id="selectByRoleId" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where roleId= #{userid,jdbcType=INTEGER}
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Aug 30 17:45:39 CST 2021.
-->
select
<include refid="Base_Column_List" />
from user
where userId = #{userid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Aug 30 17:45:39 CST 2021.
-->
delete from user
where userId = #{userid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.zhou.pojo.User" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Aug 30 17:45:39 CST 2021.
-->
insert into user (userId, userName, password,
roleId)
values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{roleid,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.zhou.pojo.User" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Aug 30 17:45:39 CST 2021.
-->
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userid != null" >
userId,
</if>
<if test="username != null" >
userName,
</if>
<if test="password != null" >
password,
</if>
<if test="roleid != null" >
roleId,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userid != null" >
#{userid,jdbcType=INTEGER},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
#{password,jdbcType=VARCHAR},
</if>
<if test="roleid != null" >
#{roleid,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.zhou.pojo.User" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Aug 30 17:45:39 CST 2021.
-->
update user
<set >
<if test="username != null" >
userName = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
password = #{password,jdbcType=VARCHAR},
</if>
<if test="roleid != null" >
roleId = #{roleid,jdbcType=INTEGER},
</if>
</set>
where userId = #{userid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.zhou.pojo.User" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Mon Aug 30 17:45:39 CST 2021.
-->
update user
set userName = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
roleId = #{roleid,jdbcType=INTEGER}
where userId = #{userid,jdbcType=INTEGER}
</update>
</mapper>
友情提示!
- Mapper.java名称和Mapper.xml一致
- Mapper.java中的方法在Mapper.xml中均被实现
- Mapper.xml中的namespace为Mapper.java的应用路径
原本:
<mappers>
<package name="com.zhou.mapper"/>//mapper所在包
</mappers>
现在:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhou.mapper"/>
<property name="sqlSessionFactoryBeanName" value="factory"/>
</bean>
四、使用
原本:
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
User user=new User();
userMapper.insert(user);
现在:
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
写在最后
有什么问题欢迎一起交流