spring5--整合mybatis

mybatis的配置文件 

 

mybatis的配置文件有两种:

  1.主配置文件

  2.sql映射文件

       

 mybatis的配置文件主要配置

  别名

  sql映射文件的位置

 

spring整合mybatis

 

需要把1数据源,2事物,3sqlsessionfactory,4动态代理对象 交给spring管理

 

整合需要导入的jar包

1.spring相关jar

2.mybatis的jar

3.mybatis整合spring的jar

4.数据源(spring-jdbc-5.0.11.RELEASE.jar)

5.mysql的数据库驱动(mysql-connector-java-5.1.47.jar)

 

spring5--整合mybatis_第1张图片

 

 

 

整合过程

1.建表

2.定义表对应的对象,对象名和表名,属性和列名都一样,省了mybatis ,resultmap的配置

3.定义Dao对象和sql映射文件

5.定义mybatis的主配置文件

6.定义service对象,注入dao对象

7.定义spring的配置文件

  注册数据库,访问数据库

  注册sqlsessionfactory对象,创建sqlsessionfactory对象

  注册动态代理对象,使用mubatis的动态代理生成dao对象

  注册自定义的service对象,注入dao对象。

 

spring5--整合mybatis_第2张图片

 

demo

 

package com.cn.vo;

public class Student {
    
    private Integer id;
    private String name;
    private Integer age;
    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 Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
}




package com.cn.dao;

import com.cn.vo.Student;

public interface StudentDao {
    
    int insertStud(Student stu);
}





"1.0" encoding="UTF-8" ?>
DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
namespace="com.cn.dao.StudentDao">
    
    "insertStud">
        insert into student (name,age) values (#{name},#{age})
    




"1.0" encoding="UTF-8"?>
DOCTYPE configuration 
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

    
    
        
        "com.cn.vo"/>
    
    
    
        
        "com.cn.dao"/>
        
    
    






package com.cn.service;

import com.cn.vo.Student;

public interface StudentService {
    int addStu(Student stu); 
}




package com.cn.service;

import com.cn.dao.StudentDao;
import com.cn.vo.Student;

public class StudentServiceImpl implements StudentService{

    private StudentDao sDao ;
    @Override
    public int addStu(Student stu) {
        return sDao.insertStud(stu);
    }
    public StudentDao getsDao() {
        return sDao;
    }
    public void setsDao(StudentDao sDao) {
        this.sDao = sDao;
    }

}





"1.0" encoding="UTF-8"?>

"http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    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/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd"
    >
    
     "myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         
         "driverClassName" value="com.mysql.jdbc.Driver"/>
         "url" value="jdbc:mysql://127.0.0.1:3306/test"/>
         "username" value="root"/>
         "password" value="admin"/>
     
     
     
     
     "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         "dataSource" ref="myDataSource"/>
         
         "configLocation" value="classpath:mybatis.xml"/>
     
     
     class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            
        "sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 
        
        "basePackage" value="com.cn.dao"/>
     
    
    
    "myService" class="com.cn.service.StudentServiceImpl">
    
        "sDao" ref="studentDao"/>
    
    




package com.cn.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cn.service.StudentService;
import com.cn.vo.Student;

public class Test {

    public static void main(String[] args) {
        String resource = "applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
        StudentService service=  (StudentService) ac.getBean("myService");
        Student stu = new Student();
        stu.setName("zhangsan");
        stu.setAge(24);
        service.addStu(stu);
        
    }
}

 

spring5--整合mybatis_第3张图片

 

package com.cn.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cn.service.StudentService;
import com.cn.vo.Student;

public class Test {

    public static void main(String[] args) {
        String resource = "applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
        StudentService service=  (StudentService) ac.getBean("myService");
        Student stu = new Student();
        stu.setName("zhangsan1");
        stu.setAge(24);
//        service.addStu(stu);
        
        String[] strs = ac.getBeanDefinitionNames();
        
        for (String s : strs) {
            System.out.println(s);
        }
    }
}



myDataSource
sqlSessionFactory
org.mybatis.spring.mapper.MapperScannerConfigurer#0
myService
studentDao
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory

 

拆出来数据库的配置

 

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.user=root
jdbc.pwd=admin



    "classpath:jdbc.properties"/>
    
     "myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         
         "driverClassName" value="${jdbc.driver}"/>
         "url" value="${jdbc.url}"/>
         "username" value="${jdbc.user}"/>
         "password" value="${jdbc.pwd}"/>
     

 

目录

spring5--整合mybatis_第4张图片

 

其他的数据源

上面的数据源并不是数据库连接池

BDCP

导入两个JAR包

 

     
     "myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
         
         "driverClassName" value="${jdbc.driver}"/>
         "url" value="${jdbc.url}"/>
         "username" value="${jdbc.user}"/>
         "password" value="${jdbc.pwd}"/>
     

 

c3p0

 

      
     "myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         
         "driverClass" value="${jdbc.driver}"/>
         "jdbcUrl" value="${jdbc.url}"/>
         "user" value="${jdbc.user}"/>
         "password" value="${jdbc.pwd}"/>
     

 

spring5--整合mybatis_第5张图片

 

 

转载于:https://www.cnblogs.com/llq1214/p/11285939.html

你可能感兴趣的:(spring5--整合mybatis)