Java从入门到入土之Spring-mybtis整合篇

spring-mybatis整合需要jarbao
spring jar包
Java从入门到入土之Spring-mybtis整合篇_第1张图片mybatis jar包,注意mybatis-spring连接包版本有要求
Java从入门到入土之Spring-mybtis整合篇_第2张图片
配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    <context:component-scan base-package="com"/>
    
    <context:property-placeholder location="dataSource.properties"/>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.name}" />
        <property name="password" value="${jdbc.password}" />
    bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource"/>
        
        <property name="mapperLocations" value="classpath:com/mapper/*Mapper.xml">property>
    bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        
        <property name="basePackage" value="com.mapper"/>
    bean>
beans>

daraSource 数据库信息

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/forum
jdbc.name=root
jdbc.password=123456

Mapper接口

package com.mapper;

import com.pojo.Student;

import java.util.List;

public interface StudentMapper {

    List<Student> selectAll();
}

Mapper映射文件



<mapper namespace="com.mapper.StudentMapper">
    <resultMap type="com.pojo.Student" id="student">
        <id property="sid" column="sid"/>
        <result property="name" column="name"/>
    resultMap>
    <select id="selectAll" resultMap="student">
        select sid,name from student
    select>
mapper>

Service层代码

package com.service;

import com.mapper.StudentMapper;
import com.pojo.Student;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/*指定该类为Service层*/
@Service
public class StudentService {

    @Resource
    private StudentMapper studentMapper;

    public List<Student> getAll(){
        List<Student> students = studentMapper.selectAll();
        return students;
    }
}

测试类

package com.test;


import com.pojo.Student;
import com.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Test1 {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService studentService = (StudentService) ac.getBean("studentService");
        List<Student> all = studentService.getAll();
        System.out.println(all);
    }
}

运行结果

2019-07-25 16:33:12  [ main:1 ] - [ DEBUG ]  ==>  Preparing: select sid,name from student 
2019-07-25 16:33:12  [ main:42 ] - [ DEBUG ]  ==> Parameters: 
2019-07-25 16:33:12  [ main:64 ] - [ DEBUG ]  <==      Total: 2
[Student [sid=1, name=赵六], Student [sid=2, name=赵武]]

Process finished with exit code 0

你可能感兴趣的:(Java从入门到入土之Spring-mybtis整合篇)