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)]
在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();
}
标签:xml,整合,Spring,StudentMapper,Student,Mybatis,org,public,queryStudentByStuno 来源: https://blog.csdn.net/qq_45387521/article/details/115102817