mybatis-spring 的整合_配置application.xml

spring的配置文件文件

application.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx
     	   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 自动扫描包 -->
	<context:component-scan base-package="cn.tiger.smartsign"
		use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Service" />
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Repository" />
	</context:component-scan>
	
	<!-- 数据库配置文件位置 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	
	<!-- 配置dbcp数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 队列中的最小等待数 -->
		<property name="minIdle" value="${jdbc.minIdle}"></property>
		<!-- 队列中的最大等待数 -->
		<property name="maxIdle" value="${jdbc.maxIdle}"></property>
		<!-- 最长等待时间,单位毫秒 -->
		<property name="maxWait" value="${jdbc.maxWait}"></property>
		<!-- 最大活跃数 -->
		<property name="maxActive" value="${jdbc.maxActive}"></property>
		<property name="initialSize" value="${jdbc.initialSize}"></property>
	</bean>
	
	<!-- 全局事务配置 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- 使用annotation注解方式配置事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<!-- 配置mybitasSqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath*:cn/tiger/smartsign/xml/*.xml" />
		<property name="typeAliasesPackage" value="cn.tiger.smartsign.entity" />
	</bean>
	
	<!-- 自动扫描Mapper,允许自动注入(根据类型匹配) -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.tiger.smartsign.dao" />
	</bean>

</beans>


<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath*:cn/tiger/smartsign/xml/*.xml" />
		<property name="typeAliasesPackage" value="cn.tiger.smartsign.entity" />
</bean>

mapperLocations : 指定mybatis的xml映射器文件的位置 ,加载所在包下的所用mybatis映射器文件

typeAliasesPackage: 别名类型包-->扫描包中的所有类,并批量设置类型别名 例如 :

cn.tiger.smartsign.entity.XXX.java-->alias="XXX"

<!-- 自动扫描Mapper,允许自动注入(根据类型匹配) -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.tiger.smartsign.dao" />
	</bean>

自动扫描映射所对应的接口包

MapperScnnerConfigurer:会将类路径下的映射器并自动将他们创建成MapperFactoybean,并自动装配

basePackage:属性为映射器接口文件设置基本的包路径,可以用分号或逗号作为分隔符设置多个路径






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是 Spring Boot 整合 MyBatis-Plus 进行分页查询的示例代码: 首先,需要在 pom.xml 中添加 MyBatis-Plus 和 MySQL 的依赖: ```xml <dependencies> <!-- MyBatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> ``` 然后,需要在 application.properties 中配置 MySQL 数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 接下来,创建一个实体类和对应的 Mapper 接口: ```java @Data public class User { private Long id; private String name; private Integer age; private String email; } @Mapper public interface UserMapper extends BaseMapper<User> { IPage<User> selectUserPage(Page<User> page, @Param("name") String name); } ``` 其中,User 类是实体类,UserMapper 是 Mapper 接口,继承了 MyBatis-Plus 的 BaseMapper 接口。 注意,在 UserMapper 接口中,我们定义了一个名为 selectUserPage 的方法,它接受一个 Page 对象和一个 name 参数,返回一个 IPage 对象。这个方法将在 Controller 中调用,用于分页查询用户信息。 最后,编写 Controller 类,通过调用 UserMapper 中定义的 selectUserPage 方法来进行分页查询: ```java @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public IPage<User> getUserList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) String name) { Page<User> page = new Page<>(pageNum, pageSize); return userMapper.selectUserPage(page, name); } } ``` 在 getUserList 方法中,我们首先创建了一个 Page 对象,表示要查询的分页信息。然后,调用 userMapper.selectUserPage 方法来进行分页查询,并将结果返回给前端。 最后,这里提供一个 Mapper.xml 文件的示例,用于实现分页查询: ```xml <select id="selectUserPage" resultMap="BaseResultMap"> select * from user <if test="name != null"> where name like concat('%', #{name}, '%') </if> <if test="orderBy != null"> order by ${orderBy} </if> </select> ``` 其中,selectUserPage 方法的实现与上面的示例代码类似,这里不再赘述。注意,这里使用了 MyBatis 的动态 SQL 功能,根据传入的参数来动态生成 SQL 语句。具体来说,如果传入了 name 参数,则会在 SQL 语句中添加一个 where 子句,用于按照用户名进行模糊查询;如果传入了 orderBy 参数,则会在 SQL 语句中添加一个 order by 子句,用于按照指定字段排序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值