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)

 

 

 

 

整合過程

1.建表

2.定義表對應的對象,對象名和表名,屬性和列名都一樣,省了mybatis ,resultmap的配置

3.定義Dao對象和sql映射文件

5.定義mybatis的主配置文件

6.定義service對象,注入dao對象

7.定義spring的配置文件

  注冊數據庫,訪問數據庫

  注冊sqlsessionfactory對象,創建sqlsessionfactory對象

  注冊動態代理對象,使用mubatis的動態代理生成dao對象

  注冊自定義的service對象,注入dao對象。

 

 

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);
}





<?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.cn.dao.StudentDao">
    <!-- SQL語句 -->
    <insert id="insertStud">
        insert into student (name,age) values (#{name},#{age})
    </insert>
</mapper>



<?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>
    <!-- 別名 -->
    <typeAliases>
        <!-- 表示這個包下的所有類的 類名就是別名 -->
        <package name="com.cn.vo"/>
    </typeAliases>
    <!-- sql映射文件的位置 -->
    <mappers>
        <!-- name 是dao接口的包名,表示 這個包下的所有映射文件都能找到 要求:1.sql映射文件名和dao接口名一樣 2.sql映射文件和dao接口在同一目錄 -->
        <package name="com.cn.dao"/>
        
    </mappers>
    <!-- 數據源和事物 不需要了 -->
</configuration>





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;
    }

}





<?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: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"
    >
    <!--  
        注冊數據源 3個數據源 ①spring提供的
     -->
     <bean id ="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <!-- 設置注入,提供數據庫的連接信息 -->
         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
         <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
         <property name="username" value="root"/>
         <property name="password" value="admin"/>
     </bean>
     
     
     <!--和mybatis有關的 注冊SqlSessionFactoryBean 
         單獨使用過 mybatis的時候,創建SqlSessionFactoryBean 需要讀主配置文件的。主配置文件包括 別名,sql映射文件的位置,數據源。
         現在需要將數據源信息賦值
     -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <property name="dataSource" ref="myDataSource"/>
         <!-- 指定mybatis主配置文件的位置 classpath spring 遇到這個,就會轉為當前的類路徑src下 -->
         <property name="configLocation" value="classpath:mybatis.xml"/>
     </bean>
     <!-- 注冊動態代理對象 使用mybatis的動態代理技術,創建Dao對象 這個id不需要。因為這個類是產生dao對象的,代碼中不會用到的 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置 sqlSessionFactory -->    
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 
        <!-- 告訴這個類,去哪里找dao接口。配置dao接口所在的包名 -->
        <property name="basePackage" value="com.cn.dao"/>
     </bean>
    
    <!-- 注冊service對象 -->
    <bean id="myService" class="com.cn.service.StudentServiceImpl">
    <!-- 默認 創建的dao對象  是類名 首字母小寫
        例外: 當類名有兩個大寫字母時,此時創建的 就是賦值給 類名
     -->
        <property name="sDao" ref="studentDao"/>
    </bean>
    
</beans>



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);
        
    }
}

 

 

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


<!-- 引入屬性配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--  
        注冊數據源 3個數據源 ①spring提供的
     -->
     <bean id ="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <!-- 設置注入,提供數據庫的連接信息 -->
         <property name="driverClassName" value="${jdbc.driver}"/>
         <property name="url" value="${jdbc.url}"/>
         <property name="username" value="${jdbc.user}"/>
         <property name="password" value="${jdbc.pwd}"/>
     </bean>

 

目錄

 

其他的數據源

上面的數據源並不是數據庫連接池

BDCP

導入兩個JAR包

 

     <!--  
        注冊數據源 dbcp連接池
     -->
     <bean id ="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
         <!-- 設置注入,提供數據庫的連接信息 -->
         <property name="driverClassName" value="${jdbc.driver}"/>
         <property name="url" value="${jdbc.url}"/>
         <property name="username" value="${jdbc.user}"/>
         <property name="password" value="${jdbc.pwd}"/>
     </bean>

 

c3p0

 

      <!--  
        注冊數據源 c3p0連接池
     -->
     <bean id ="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <!-- 設置注入,提供數據庫的連接信息 -->
         <property name="driverClass" value="${jdbc.driver}"/>
         <property name="jdbcUrl" value="${jdbc.url}"/>
         <property name="user" value="${jdbc.user}"/>
         <property name="password" value="${jdbc.pwd}"/>
     </bean>

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM