springMVC配置持久层的方式

1. 利用接口配置

配置接口的方式实现持久层

<!-- @declare: 配置dao层扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.silencer.web.dao" />
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

接口配置

@Repository
public interface UserMapper extends BaseMapper<User> {}

2. 配置sqlSessionTemplate

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
@Repository
public class BaseServiceSession {
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;

	public Result queryPage(int pageNo, int pageSize, String sql, String sqlCount, Object obj) {
		RowBounds row = new RowBounds(pageNo, pageSize);
		List<?> list = sqlSessionTemplate.selectList(sql, obj, row);
		int count = sqlSessionTemplate.selectOne(sqlCount, obj);
		return Result.success(new Page(list, count));
	}

}

3. 继承SqlSessionDaoSupport类

@Repository
public class BaseService extends SqlSessionDaoSupport {

	@Autowired
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}

	public Result queryPage(int pageNo, int pageSize, String sql, String sqlCount, Object obj) {
		RowBounds row = new RowBounds(pageNo, pageSize);
		List<?> list = this.getSqlSession().selectList(sql, obj, row);
		int count = this.getSqlSession().selectOne(sqlCount, obj);
		return Result.success(new Page(list, count));
	}
	
}

第三种方式用到的实际是第二种方式中的sqlSessionTemplate

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC 中的持久层通常指的是数据访问层,使用 Spring JDBC、JPA 或 Hibernate 等技术。这里我会简单概述一下如何编写 Spring MVC 与 JPA(Java Persistence API)结合的持久层代码: 1. 添加依赖:在 Maven 或 Gradle 项目中添加 JPA 和 Hibernate 的依赖。 ```xml <!-- Maven --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Gradle (Kotlin) --> implementation 'org.springframework.boot:spring-boot-starter-data-jpa' ``` 2. 配置数据源:在 `application.properties` 或 `application.yml` 中配置数据库连接信息。 ```properties spring.datasource.url=jdbc:mysql://localhost:3306/your_database spring.datasource.username=your_username spring.datasource.password=your_password spring.jpa.hibernate.ddl-auto=update ``` 3. 定义 Entity 类(实体类):继承 `javax.persistence.Entity` 并添加属性和 ID。 ```java @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; // getters and setters } ``` 4. 创建 Repository 接口:使用 Spring Data JPA 提供的仓库模式。 ```java import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User, Long> { // CRUD operations User findByEmail(String email); } ``` 5. Service 层:注入 Repository,并执行业务逻辑。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { private final UserRepository userRepository; @Autowired public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public User getUserByEmail(String email) { return userRepository.findByEmail(email); } // Other methods... } ``` 6. 控制器层:调用 Service 方法并处理请求。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @GetMapping("/users/{email}") public User getUser(@PathVariable("email") String email) { return userService.getUserByEmail(email); } // Other endpoints... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值