首页 > 代码库 > MyBatis+Spring整合

MyBatis+Spring整合

不多说,直接上案例!

1. 建立数据库+表

CREATE TABLE s_user(
    user_id INT AUTO_INCREMENT PRIMARY KEY,
    user_name VARCHAR(30),
    user_birthday DATE,
    user_salary DOUBLE
)

2. 加包

  • myBatis包:

  技术分享

  • spring包

  技术分享

  技术分享

  • MySQL驱动包

  技术分享

3. 建立实体类

package com.atguigu.ms.domain;

import java.util.Date;

public class User {
    
    private int id;
    private String name;
    private Date birthday;
    private double salary;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    public User(int id, String name, Date birthday, double salary) {
        super();
        this.id = id;
        this.name = name;
        this.birthday = birthday;
        this.salary = salary;
    }
    public User() {
        super();
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", salary=" + salary + "]";
    }
    

}

4. 编写Spring的beans.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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- 数据源 -->
    <bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/mybatis1"></property>
        <property name="username" value="root"></property>
        <property name="password" value="lfdy"></property>
    </bean>
    
    <!-- mybatis的SqlSessionFactory工厂 -->
    <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="driverManagerDataSource"></property>
        <property name="typeAliasesPackage" value="com.atguigu.ms.domain"></property>
        <property name="configLocation" value="classpath:mybatis-spring.xml"></property>
    </bean>
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.atguigu.ms.mapper.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory1"></property> </bean> </beans>

5.编写UserDao.java文件

package com.atguigu.ms.mapper;

import java.util.List;

import com.atguigu.ms.domain.User;

/**
 * @author hp
 *
 */
public interface UserMapper {

    void save(User user);
    
    void update(User user);
    
    void delete(int id);
    
    User findById(int id);
    
    List<User> findAll();
    
}

6. 编写sql映射文件userMapper.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">
    
    <!-- 
        namespace : 必须与对应的接口全类名一致 
        id:    必须与对应接口的某个对应的方法名一致
    -->

    <mapper namespace="com.atguigu.ms.mapper.UserMapper">
        
        <resultMap type="User" id="userResult">
            <result column="user_id" property="id"/>
            <result column="user_name" property="name"/>
            <result column="user_birthday" property="birthday"/>
            <result column="user_salary" property="salary"/>
        </resultMap>
        
        <insert id="save" keyColumn="user_id" keyProperty="id" useGeneratedKeys="true">
            insert into s_user(user_name,user_birthday,user_salary)
             values(#{name},#{birthday},#{salary})
        </insert>
        
        <update id="update">
            update s_user set user_name=#{name},user_birthday=#{birthday},user_salary=#{salary}
            where user_id = #{id}
        </update>
        
        <delete id="delete">
            delete from s_user
            where user_id = #{id}
        </delete>
        
        <select id="findById" resultMap="userResult">
            select *
            from s_user
            where user_id = #{id}
        </select>
    
        <select id="findAll" resultMap="userResult">
            select * 
            from s_user
        </select>
        
        
        
    </mapper>
    

7. 编写spring-mybatis.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>
    <mappers>
        <mapper resource="com/atguigu/ms/mapper/userMapper.xml"/>
    </mappers>
</configuration>

8.测试

package com.atguigu.ms.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.atguigu.ms.domain.User;
import com.atguigu.ms.mapper.UserMapper;

public class Main {
    
    
    public static void main(String[] args){
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        
        UserMapper userMapper = (UserMapper) ctx.getBean("userDao");
    
        Object obj = userMapper.findById(1).toString();
        System.out.println(obj);
    }

}

 

 

 

 

 

 

     

 

MyBatis+Spring整合