个人主页: 几分醉意的CSDN博客_传送门

文章目录

  • 集成思路
    • ✨怎么使用MyBatis
    • ✨集成的步骤
    • ✨pom加入依赖
    • ✨创建MyBatis使用代码
    • ✨创建Service类
    • ✨创建Spring配置文件和测试集成MyBatis
    • ✨使用外部属性配置文件
  • 图书推荐 Java28岁了!这些好书推荐给你
    • ✨Java语言程序设计(原书第12版)
    • ✨Java核心技术(原书第11版)
    • ✨Java核心技术(原书第12版)
    • ✨培养Java编程思维
    • ✨Effective Java
    • ✨Java并发编程实战
  • 参加方式
  • 投票(传送门)

集成思路

spring能集成很多的框架,是spring一个优势功能。通过集成功能,让开发人员使用其他框架更方便。集成使用的是spring ioc 核心技术。

✨怎么使用MyBatis

使用mybatis,需要创mybatis框架中的某些对象,使用这些对象,就能使用mybatis提供的功能了。

分析: mybatis执行sql语句,需要使用那些对象? 1. 需要有Dao接口的代理对象,例如StudentDao接口,需要一个它的代理对象,使用 SqlSession.getMapper(StudentDao.class),得到dao代理对象。 2. 需要有SqlSessionFactory,创建SqlSessionFactory对象,才能使用openSession(得到SqlSession对象。 3. 数据源DataSource对象,使用一个更强大,功能更多的连接池对象代替mybatis自己的PooledDataSource。

✨集成的步骤

实现步骤:1.使用的mysql库, 使用学生表 student2(id int 主键列, 自动增长,name varchar(80),age int)2.创建maven项目3.加入依赖gavspring依赖, mybatis依赖, mysql驱动。 junit依赖mybatis-spring依赖(mybatis网站上提供的,用来在spring项目中,创建mybatis对象)spring有关事务的依赖。mybatis和spring整合的时候, 事务是自动提交的。4.创建实体类Student5.创建Dao接口和mapper文件写sql语句6.写mybatis主配置文件7.创建service接口和他的实现类8.创建spring的配置文件1)声明数据源DataSource,使用的阿里的Druid连接池2) 声明SqlSessionFactoryBean类,在这个类内部创建的是SqlSessionFactory对象。3)声明MapperScannerConfiguration类,在内部创建dao代理对象, 创建的对象都放在spring容器中。4)声明Service对象,把3)的中dao赋值给service属性9.测试dao访问数据库

✨pom加入依赖

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.1</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.9</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.12</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build></project>

✨创建MyBatis使用代码

1.创建实体类,生成get、set和toString方法。

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;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';}}

2.创建Dao接口,在里面写方法。

public interface StudentDao {//添加操作int insertStudent(Student student);//查询操作List<Student> selectStudents();}

3.创建mapper文件,写SQL。

<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="youfei1_v.Dao.StudentDao"><insert id="insertStudent" >insert into student2(name,age) values (#{name},#{age})</insert><select id="selectStudents" resultType="youfei1_v.domain.Student">select id,name ,age from student2</select></mapper>

4.创建Mybatis主配置文件。

注意:Spring集成MyBatis,MyBatis主配置文件里面不需要指定数据源了,下面会介绍在哪里指定。基本上Mybatis主配置文件里面就设置个日志、别名和其它的mapper文件。
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings><mappers><!----><package name="youfei1_v.Dao"/></mappers></configuration>

✨创建Service类

1.创建service接口。

public interface StudentService {int addStudent(Student student);List<Student> queryStudent();}

1.实现service接口。

public class StudentServiceImpl implements StudentService {private StudentDao studentDao; //创建这个dao对象和set方法public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;}@Overridepublic int addStudent(Student student) {int rows = studentDao.insertStudent(student); //方法中调用studentDao对象的方法return rows;}@Overridepublic List<Student> queryStudent() {List<Student> students = studentDao.selectStudents();return students;}}

✨创建Spring配置文件和测试集成MyBatis

1.创建Spring配置文件。

<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="classpath:jdbc.properties"/><bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="url" value="jdbc:mysql://localhost:3306/springdb"/><property name="username" value="root"/><property name="password" value="123456"/></bean><bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="myDataSource" /> <property name="configLocation" value="classpath:mybatis.xml" /></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="factory" /><property name="basePackage" value="youfei1_v.Dao" /></bean><bean id="studentService" class="youfei1_v.service.impl.StudentServiceImpl"><property name="studentDao" ref="studentDao" /> </bean> </beans>

2.测试

✨使用外部属性配置文件

1.创建一个.properties的文件

2.在spring配置文件中引用

图书推荐 Java28岁了!这些好书推荐给你

1995年5月23日,Java带着开发团队对它的宏伟愿景诞生了。在28年中,Java给我们的世界创造了一个又一个的精彩。作为一种最流行的网络编程语言之一,Java语言在当今信息化社会中发挥了重要的作用。虽然软件开发行业语言种类很多,但是Java工程师的需求量占据了软件开发工程师总需求量的60%-70%。

今天就是 #Java# 28岁了,博主为你推荐几本百万开发者都在看的Java经典名著,伴你顺利晋级高阶程序员!

✨Java语言程序设计(原书第12版)


Java经典教材再推新版,畅销20余年,被世界各地的大学选作教材,更新至Java9、10和11,涵盖Java新特性。本书通过示例讲解问题求解技巧,提供大量的程序清单,每章配有丰富的复习题和编程练习题,帮助读者掌握编程技术并解决实际开发中遇到的问题。
————————————————————

✨Java核心技术(原书第11版)


极具影响力的巅峰之作!历久弥新,累计销量超100万册!包含Java SE 9、10和11新特性。畅销20载的大师之作,Jolt大奖得主,全球百万Java工程师口碑选择,Java事实标准,提供部分作者亲授视频+海量示例代码集。
————————————————————

✨Java核心技术(原书第12版)


经典畅销书CoreJava根据Java17新特性全面升级!系统全面、深入理解Java核心技术。如您刚入门Java,或者准备升级到Java17,建议直接选用第12版学习。
————————————————————

✨培养Java编程思维


永不过时的Java编程思想教科书!学习Java必读经典图书!超具亲和力的文字和小而直接的编程示例将晦涩难懂的概念讲透,从Java的基础语法到最高级特性,指导你轻松掌握。
————————————————————

✨Effective Java


适合已经掌握Java核心技术的程序员,想更加深入地了解Java编程语言的开发者阅读。针对如何编写高效、设计优良的程序提出了最实用、最权威的指导方针,通过90条简短、独立的经验法则,探索新的设计模式和语言习惯用法,帮你更加有效地使用Java编程语言及其基本类库,指引你少走弯路。
————————————————————

✨Java并发编程实战


本书是Java并发编程领域的里程碑著作!从并发编程的基本理论入手,逐步介绍了在设计Java并发程序时各种重要的设计原则、设计模式与思维模式。
————————————————————

参加方式

本次送书4本,在评论区抽三位小伙伴,评论点赞数最高获得一本
活动时间:截止到 2023-05-31 22:00:00(下周三开奖),中奖的小伙伴以上六本任选一本包邮到家
抽奖方式:在线抽奖工具进行抽奖3位同学,评论点赞数最高的同学获得一本
参与方式:关注博主、点赞、收藏,评论区评论 “java生日快乐”
(多条增加权重,最多五条有效哦)
通知方式:通过动态与私信与本文最后同时公布
领奖方式:获得后加我微信发我地址即可

————————————————

投票(传送门)