spring中mybatis怎么配
-
在Spring中配置MyBatis可以经过以下步骤:
-
导入相关依赖:在Maven或者Gradle中添加MyBatis和Spring相关的依赖。
<!-- MyBatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- Spring依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> -
创建数据库连接配置:在application.properties或application.yml中配置数据库连接信息。
# 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver -
创建MyBatis配置文件:创建一个名为mybatis-config.xml的配置文件,用于配置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> <!-- 配置类型别名 --> <typeAliases> <package name="com.example.entity" /> </typeAliases> </configuration> -
创建MyBatis映射文件:创建一个名为xxxMapper.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="com.example.mapper.UserMapper"> <!-- 配置SQL语句和映射关系 --> <select id="getUserById" resultType="com.example.entity.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper> -
创建MyBatis接口:创建一个接口,用于定义SQL查询的方法。
package com.example.mapper; import com.example.entity.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper public interface UserMapper { User getUserById(@Param("id") Long id); } -
创建Spring配置文件:创建一个名为applicationContext.xml的Spring配置文件,用于整合MyBatis和Spring。
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 扫描MyBatis的接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper" /> </bean> <!-- 配置MyBatis的SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- 配置Spring的事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启Spring的事务注解 --> <context:annotation-config /> <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" /> </beans> -
创建Spring Boot启动类:创建一个带有@SpringBootApplication注解的启动类。
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
以上就是在Spring中配置MyBatis的基本步骤,配置完成后即可在代码中使用MyBatis进行数据库操作。
1年前 -
-
在Spring中配置MyBatis可以按照以下步骤进行:
- 添加依赖:在pom.xml文件中添加MyBatis和MyBatis-Spring的依赖项。例如:
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> </dependencies>- 配置数据源:在Spring的配置文件(例如applicationContext.xml)中配置数据库连接信息和数据源。例如使用Spring的内置数据源配置:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mydatabase" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean>- 配置SqlSessionFactoryBean:配置SqlSessionFactoryBean,并将数据源和MyBatis的配置文件关联起来。例如:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean>- 配置MapperScannerConfigurer:配置MapperScannerConfigurer来自动扫描并注册Mapper接口。例如:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper" /> </bean>在该示例中,将自动扫描com.example.mapper包下的接口,并将其注册为MyBatis的Mapper。
- 编写MyBatis的配置文件(mybatis-config.xml):在classpath下创建mybatis-config.xml文件,并在该文件中配置MyBatis的一些全局属性,例如数据库配置、类型别名、插件等。
以上是在Spring中配置MyBatis的基本步骤,根据具体的项目需求,还可以进行更多配置,并使用注解来简化配置过程。同时,还需要在Spring中配置事务管理器,以确保MyBatis的操作在事务中进行。
1年前 -
在Spring中配置MyBatis,需要完成以下几个步骤:
- 引入相关依赖
首先,在项目的pom.xml文件中引入MyBatis和MyBatis-Spring的依赖。例如:
<dependencies> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <!-- MyBatis-Spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!-- 其他依赖... --> </dependencies>- 配置数据源
在Spring的配置文件中,配置数据源。例如,使用MyBatis和MySQL的组合,可以使用Spring自带的org.springframework.jdbc.datasource.DriverManagerDataSource类配置数据源,如下所示:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mydatabase" /> <property name="username" value="username" /> <property name="password" value="password" /> </bean>- 配置SqlSessionFactoryBean
在Spring的配置文件中,配置SqlSessionFactoryBean。SqlSessionFactoryBean是MyBatis中的核心类,用于创建SqlSession。例如:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.example.model" /> <property name="mapperLocations" value="classpath*:mapper/*.xml" /> <!-- 其他配置... --> </bean>其中,
typeAliasesPackage用于指定实体类的包路径,mapperLocations用于指定映射文件(Mapper XML)的路径。- 配置MapperScannerConfigurer
在Spring的配置文件中,配置MapperScannerConfigurer。MapperScannerConfigurer用于扫描Mapper接口,并将其注册为Bean。例如:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>其中,
basePackage用于指定Mapper接口的包路径,sqlSessionFactoryBeanName用于指定SqlSessionFactoryBean的名称。- 编写Mapper接口和映射文件
最后,根据需要编写Mapper接口和映射文件。Mapper接口定义了数据访问的方法,映射文件定义了SQL语句和映射规则。例如:
public interface UserMapper { User getUserById(Integer id); } <!-- UserMapper.xml --> <mapper namespace="com.example.dao.UserMapper"> <select id="getUserById" resultType="com.example.model.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper>以上就是在Spring中配置MyBatis的基本步骤。通过正确配置以上内容,就可以在Spring中使用MyBatis进行数据访问了。
1年前 - 引入相关依赖