ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring 集成 MyBatis

2021-12-27 19:02:35  阅读:117  来源: 互联网

标签:集成 String service Spring void sex MyBatis public name


  • Spring和MyBatis整合时,MyBatis的事务是自动提交的

项目架构

第一步 创建做为数据载体的domain

public class Student {
    public String id;
    public String name;
    public int age;
    public String sex;

    public Student() {
    }

    public Student(String id, String name, int age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

第二步 创建与数据交互的dao对象

public interface StudentDao {
    List<Student> selectAll();
}

创建对象的同时完成 dao.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="dao.StudentDao">

    <select id="selectAll" resultType="domain.Student">
        select * from student;
    </select>

</mapper>


第三步 创建service接口和对象
service接口

public interface StudentService {
    void selectAll();
}

接口实现类

public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;

    public void setStudentDao(StudentDao studentDao){
        this.studentDao = studentDao;
    }

    public void selectAll(){
        System.out.println("Executing selectAll-----------------------");
        List<Student> students = studentDao.selectAll();
        for(Student stu:students){
            System.out.println(stu.name);
        }
    }
}

第四步 创建MyBatis主配置文件

<?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>

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/><!--开启日志-->
    </settings>

    <typeAliases>
        <package name="dao"/>
    </typeAliases>

    <mappers>
        <package name="dao"/>
    </mappers>
</configuration>

第五步 创建spring的配置文件:声明mybatis的对象交给spring创建

  1. 数据源Datasource
  2. SqlSessionFactory
  3. Dao对象
  4. 声明自定义的service
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入配置文件-->
    <context:property-placeholder location="jdbc.properties"/>

    <!--Druid连接池-->
    <bean id="myDateSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <!--连接池配置信息-->
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
    </bean>

    <!--SqlSessionFactory对象-->
    <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--赋值 连接池和MyBatis主配置文件-->
        <property name="dataSource" ref="myDateSource"/>
        <property name="configLocation" value="classpath:Mybatis.xml"/>
    </bean>

    <!--创建dao对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--sqlSessionFactory赋值-->
        <property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"/>
        <!--将dao包下所有dao对象交给Spring容器管理-->
        <property name="basePackage" value="dao"/>
    </bean>
    
    <!--创建service对象-->
    <bean id="myStudentService" class="service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao"/>
    </bean>
</beans>

第六步 创建测试类

public class myTest {
    @Test
    public void test01(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService service = (StudentService) ac.getBean("myStudentService");
        service.selectAll();
    }
}

总结:项目流程就是创建好Spring 容器,Dao对象等,然后通过获取代表Spring容器的ApplicationContext对象,通过该对象获取service对象通过service.selectAll() 进行业务的进行,而在service中会获取与数据库交互的StudentDao,然后对数据库的数据进行查询,并返回。

标签:集成,String,service,Spring,void,sex,MyBatis,public,name
来源: https://blog.csdn.net/The_RedMaple/article/details/122178421

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有