Spring+Mybatis整合(详细)

Spring+Mybatis整合(详细)

第一步:jar包(16个可用maven)

使用IDEA的在WEB-INF下建立一个lib文件把jar包复制进去标记为库

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oGbcBFan-1616423000351)(C:\Users\lbw\AppData\Roaming\Typora\typora-user-images\image-20210321201715607.png)]

第二步:将数据库的类和表进行映射

创建实例类生成get/set方法无参构造和带参构造也可以生成(alt+ins生成方法的快捷方式)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PUuX4YVS-1616423000352)(C:\Users\lbw\AppData\Roaming\Typora\typora-user-images\image-20210321201854218.png)][外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YSaWcCeK-1616423000353)(C:\Users\lbw\AppData\Roaming\Typora\typora-user-images\image-20210321201904191.png)]

第三步、通过mapper.xml将类、表建立映射关系

在mybatis官方文档的2.1.5 Exploring Mapped SQL Statements

复制下来即可

注意namespace(根据包名来配置每个人的可能都不一样)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B9v8hqqE-1616423000354)(C:\Users\lbw\AppData\Roaming\Typora\typora-user-images\image-20210321202706637.png)]

第四步、创建spring的配置文件applicationContext.xml(代替mybatis的配置文件(conf.xml数据库信息在spring里面配置)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z7iKdyMy-1616423000355)(C:\Users\lbw\AppData\Roaming\Typora\typora-user-images\image-20210321203110121.png)]

​ 1.使用BasicDataSource来配置数据库信息其中有很多属性例

<!--配置数据库文件BasicDataSource(引用目录)-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="url" value=""></property>
    <property name="username" value=""></property>
    <property name="password" value=""></property>
    <property name="driverClassName" value=""></property>
</bean>

value可以直接写也可以用配置文件然后引用

引用dp.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root

使用PreferencesPlaceholderConfigurer的引用路径

在这里插入图片描述

使用属性加载dp.properties文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UpdBx4FS-在这里插入图片描述

引用SqlSessionFactoryBean的引用目录使用它自带的属性加载mapper和引用datasource

在这里插入图片描述

使用一个MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBeans。要创建MapperScannerConfigurer,可以在Spring的配置中添加如下代码:

<!--批量产生mapper映射对象
	使用一个MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBeans。
-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    <property name="basePackage" value="org.sm.mapper"></property>
</bean>

用一个案例来实现一下学生查询的方法

创建dao类以及它的实现类

dao接口写一个queryStudentByStuno的方法(由于是mapper.xml的映射文件所有名字改为StudentMapper.java)

package org.sm.mapper;

import org.sm.entity.Student;

public interface StudentMapper {
    Student queryStudentByStuno(int stuno);
}

dao类实现类

package org.sm.dao;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.sm.entity.Student;
//继承SqlSessionDaoSupport使用super.getSqlsession方法获取StudentMapper
public class Studentimpi  extends SqlSessionDaoSupport implements StudentMapper{


    @Override
    public Student queryStudentByStuno(int stuno) {
        SqlSession sqlSession = super.getSqlSession();
        //获取Mapper
        StudentMapper studentDao=sqlSession.getMapper(StudentMapper.class);
        return studentDao.queryStudentByStuno(stuno);
    }
}

继承了 “SqlSessionDaoSupport” ,利用方法 getSqlSession() 可以得到 SqlSessionTemplate ,从而可以执行各种 SQL 语句

studentMapper.xml(写sql语句)

<?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="org.ssm.mapper.studentMapper">
    <select id="queryStudentByStuno" resultType="org.sm.entity.Student" parameterType="int">
 select * from student where stuno = #{stuNo}
 </select>
</mapper>

service接口类

package org.sm.service;

import org.sm.entity.Student;

public interface IStudentService {
    Student queryStudentByStuno(int stuno);
}

service实现类

//service依赖dao(在applicationContext.xml中把Dao类放入IOC容器在注入给Service)
private StudentMapper studentMapper;

public void setStudentMapper(StudentMapper studentMapper) {
    this.studentMapper = studentMapper;
}

@Override
public Student queryStudentByStuno(int stuno) {
      //调用dao
    return studentMapper.queryStudentByStuno(stuno);
}

applicationContext.xml(set方式注入)

<!--StudentDao-->
<bean id="studentMapper" class="org.sm.dao.impi.StudentDaoimpi">
    <property name="sqlSessionFactory" ref="SqlSessionFactory"></property>
</bean>
<!--把StudentServiceImpi加入ioc容器-->
<bean id="service" class="org.sm.service.impi.StudentServiceImpI">
    <property name="studentMapper" ref="studentMapper"></property>
</bean>

测试类(test)

public  static void queryStudentByStuno(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    IStudentService studentservice = (IStudentService) context.getBean("service");
    Student student = studentservice.queryStudentByStuno(2);
    System.out.println(student);
}
public static void main(String[] args) {
    queryStudentByStuno();
}
"service");
    Student student = studentservice.queryStudentByStuno(2);
    System.out.println(student);
}
public static void main(String[] args) {
    queryStudentByStuno();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洛尘 programmer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值