首页 > 其他分享 >手牵手入门Spring6整合Mybatis3.5

手牵手入门Spring6整合Mybatis3.5

时间:2023-03-07 20:32:17浏览次数:43  
标签:www http spring springframework Mybatis3.5 手牵手 mybatis org Spring6

方式一


Object类

手牵手入门Spring6整合Mybatis3.5_spring

get和set,无参构造+有参构造

手牵手入门Spring6整合Mybatis3.5_bc_02

Pom.xml引入依赖

手牵手入门Spring6整合Mybatis3.5_xml_03

<!--    打包方式jar-->

<packaging>jar</packaging>

<!--配置多个仓库 -->

<repositories>

<!-- Spring6 -->

<repository>

<id>repository.spring.milestone</id>

<name>Spring Milestone Repository</name>

<url>https://repo.spring.io/milestone</url>

</repository>

</repositories>

<!-- 依赖-->

<dependencies>

<!-- 引入Spring context依赖-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>6.0.0-M2</version>

</dependency>

<!--引入Spring-jdbc-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>6.0.0-M2</version>

</dependency>

<!--引入mysql驱动-->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.17</version>

</dependency>

<!--引入mybatis依赖-->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.5.11</version>

</dependency>

<!--引入mybatis-spring依赖-->

<!-- -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>3.0.1</version>

</dependency>

<!-- 引入德鲁伊连接池-->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.2.15</version>

</dependency>

<!-- Spring框架对Junit支持的依赖-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>6.0.0-M2</version>

<!--6.0.0-M2既支持junit4又支持junit5-->

</dependency>

<!--junit依赖-->

<dependency>

<groupId>org.junit.jupiter</groupId>

<artifactId>junit-jupiter-api</artifactId>

<version>5.8.2</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.example</groupId>

<artifactId>review-annotation</artifactId>

<version>1.0-SNAPSHOT</version>

<scope>test</scope>

</dependency>

<!-- @Resource注解的依赖-->

<dependency>

<groupId>jakarta.annotation</groupId>

<artifactId>jakarta.annotation-api</artifactId>

<version>2.1.0</version>

</dependency>

</dependencies>

Dao层​

public interface TTTDao {

//查询所有

List<TTT11> selectAll();

//插入

int insert(TTT11 ttt11);

//通过id删除

int deleteById(int id);

//更新

int update(TTT11 ttt11);

}

myabtis-Mapper配置文件

手牵手入门Spring6整合Mybatis3.5_spring_04

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.bank.dao.TTTDao">

<select id="selectAll" resultType="com.bank.object.TTT11">

select id,name,address from TTT11

</select>
<insert id="insert">

insert into TTT11 values(#{id},#{name},#{address})

</insert>
<delete id="deleteById" >

delete from TTT11 where id = #{id}

</delete>
<update id="update">

update TTT11 set address =#{address} where id=#{id}

</update>

</mapper>

编写jdbc.properties文件和Mybatis.xml核心配置文件

jdbc.properties


手牵手入门Spring6整合Mybatis3.5_xml_05

jdbc.driver=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://IP:3306/mysql?

jdbc.user= root

jdbc.password= 111111

Mybatis.xml主配置文件

手牵手入门Spring6整合Mybatis3.5_bc_06

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"https://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
<!--settings控制mybatis全局行为-->

<settings>

<!-- logImpl设置mybatis输出日志-->

<setting name="logImpl" value="STDOUT_LOGGING"/>

</settings>
</configuration>

Service接口和serviceimp

手牵手入门Spring6整合Mybatis3.5_bc_07

接口

public interface TTTService {
int save(TTT11 ttt11);

int delecyById(int id);

List<TTT11> selectAll();

int update(TTT11 ttt11);

}

 

实现类​

@Transactional

@Service("tTTService")

public class TTTServiceimpl implements TTTService {
@Resource

private TTTDao tttDao;
@Override

public int save(TTT11 ttt11) {

return tttDao.insert(ttt11);

}
@Override

public int delecyById(int id) {

return tttDao.deleteById(id);

}
@Override

public List<TTT11> selectAll() {

return tttDao.selectAll();

}
@Override

public int update(TTT11 ttt11) {

return tttDao.update(ttt11);

}

}

Spring.xml配置


<?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" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:component-scan base-package="com.bank"></context:component-scan>

<!--引入外部文件-->

<context:property-placeholder location="jdbc.properties"/>
<bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">

<property name="driverClassName" value="${jdbc.driver}"></property>

<property name="url" value="${jdbc.url}"></property>

<property name="username" value="${jdbc.user}"></property>

<property name="password" value="${jdbc.password}"></property>

</bean>

<!--配置SqlSessionFactoryBean-->

<bean class="org.mybatis.spring.SqlSessionFactoryBean">

<!--注入数据源-->

<property name="dataSource" ref="dateSource"/>

<!--指定Mybatis-config.xml文件-->

<property name="configLocation" value="mybatis-config.xml"/>

<!--注入别名-->

<property name="typeAliasesPackage" value="com.bank.object"/>

</bean>

<!--扫描mapper配置器-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.bank.dao"/>

</bean>

<!--事务管理器-->

<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dateSource"/>

</bean>

<!-- 启动事务注解-->

<tx:annotation-driven transaction-manager="TransactionManager"/>

</beans>

测试类

手牵手入门Spring6整合Mybatis3.5_xml_08


public class springMybatisTest {

@Test
public void sMybatisTest(){
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");
TTTService tTTService = applicationContext.getBean("tTTService", TTTService.class);
tTTService.selectAll();
}
}

方式二

手牵手入门Spring6整合Mybatis3.5_spring_09

pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>spring6mybatis</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 打包方式jar-->
<packaging>jar</packaging>
<!--配置多个仓库 -->
<repositories>
<!-- Spring6 -->
<repository>
<id>repository.spring.milestone</id>
<name>Spring Milestone Repository</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<!-- 依赖-->
<dependencies>
<!-- 引入Spring context依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0-M2</version>
</dependency>
<!--引入Spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.0-M2</version>
</dependency>
<!--引入mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!--引入mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<!--引入mybatis-spring依赖-->
<!-- -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 引入德鲁伊连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.15</version>
</dependency>
<!-- Spring框架对Junit支持的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.0-M2</version>
<!--6.0.0-M2既支持junit4又支持junit5-->
</dependency>
<!--junit依赖-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<!--所在的目录-->
<directory>src/main/java</directory>
<includes>
<!--包括目录下的.properties .xml文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>

实体类

手牵手入门Spring6整合Mybatis3.5_bc_10

get、set、toString、无参有参构造方法。

接口

手牵手入门Spring6整合Mybatis3.5_spring_11

public interface TTTDao {
//查询所有
List<TTT11> selectAll();
}

mapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.TTTDao">
<select id="selectAll" resultType="com.object.TTT11">
select id,name,address from TTT11
</select>

<insert id="insert">
insert into TTT11 values(#{id},#{name},#{address})
</insert>

<delete id="deleteById" >
delete from TTT11 where id = #{id}
</delete>

<update id="update">
update TTT11 set address =#{address} where id=#{id}
</update>
</mapper>

Service接口和实现类

手牵手入门Spring6整合Mybatis3.5_xml_12

public interface TTTService {
List<TTT11> selectAll();
}
public class TTTServiceimpl implements TTTService {

private TTTDao tttDao;

public TTTServiceimpl(TTTDao tttDao) {
this.tttDao = tttDao;
}
@Override
public List<TTT11> selectAll() {
return tttDao.selectAll();
}
}

jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://IP:3306/mysql
jdbc.user= root
jdbc.password= 111111

mybatis-config.xml主配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- logImpl设置mybatis输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

<mappers>
<package name="com.dao"/>
</mappers>

</configuration>

SpringConfig.xml配置文件

<?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" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 组件扫描-->
<context:component-scan base-package="com"></context:component-scan>
<!--引入外部文件-->
<context:property-placeholder location="jdbc.properties"/>
<!-- 声明数据来源-->
<bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置SqlSessionFactoryBean-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dateSource"/>
<!--指定Mybatis-config.xml主配置文件-->
<property name="configLocation" value="mybatis-config.xml"/>
<!--注入别名-->
<property name="typeAliasesPackage" value="com.object"/>
</bean>
<!--扫描mapper配置器 SqlSession.Mapper()-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factory"></property>
<property name="basePackage" value="com.dao"/>
</bean>
<bean id="tttServiceimpl" class="com.service.TTTServiceimpl">
<!--构造注入-->
<constructor-arg index="0" ref="TTTDao"></constructor-arg>
</bean>
<!--事务管理器-->
<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dateSource"/>
</bean>
<!-- 启动事务注解-->
<tx:annotation-driven transaction-manager="TransactionManager"/>
</beans>

测试类

public class smtest {
@Test
public void test(){
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");
TTTService tttServiceimpl = applicationContext.getBean("tttServiceimpl", TTTService.class);
tttServiceimpl.selectAll();
}
}

手牵手入门Spring6整合Mybatis3.5_bc_13

标签:www,http,spring,springframework,Mybatis3.5,手牵手,mybatis,org,Spring6
From: https://blog.51cto.com/u_15081423/6106452

相关文章

  • Spring6之HTTP Interface分析
    目录1HTTPInterface1.1引言1.2示例1.2.1创建服务端1.2.2SpringBoot工程1.3深入分析1.3.1GetExchange(HttpExchange)1.3.2UserApiService实例创建1.4其他特性1HT......
  • Spring40 - Spring6 i18n
    Spring6国际化MessageSource接口spring中国际化是通过MessageSource这个接口来支持的常见实现类ResourceBundleMessageSource这个是基于Java的ResourceBundle基础类实......
  • Spring6 DI 依赖注入--Bean属性赋值
    Spring6基于XML实现Bean管理(属性赋值)IOC和DI有什么区别:DI是IOC中的具体实现,DI表示依赖注入或注入属性,注入属性要在创建对象的基础之上完成依赖注入方法bean属性赋值方......
  • 明解Spring6(一)------邂逅
    一、Spring简介1、Spring介绍Spring是一款主流的JavaEE轻量级开源框架,Spring由“Spring之父”RodJohnson提出并创立,其目的是用于简化Java企业级应用的开发难......
  • Spring6
    Spring6Spring项目的创建打开IDEA,新建一个maven项目在maven项目中引入spring的仓库和依赖<repositories><repository><id>repository.spri......
  • 【题解】P5692 [MtOI2019]手牵手走向明天
    春节前做大分块是什么奇妙传统吗……这个题好想是好想,但是写起来就是另外一回事了……思路分块。第四分块加强版。区间查询,根号分治做法寄了。看到合并颜色可以想到......
  • Spring6 正式发布!重磅更新,是否可以拯救 Java
    简介SpringFramework6和SpringBoot3是一个跨越式的升级整个框架支持的最低JDK版本直接跨越到JDK17,无论框架层还是基础设施层都做了巨大的改变,Spring6.0新框架具体......
  • Spring6.0全新发布,快来看看
    Spring6.0全新发布,快来看看加入交流群:CN.ITLTT.COMSpringFramework6.0发布了首个RC版本。翻译后页面(有点好笑):Onbehalfoftheteamandeveryonewhohas......