Spring整合mybatis-改造service层

首先添加一下Service层

package cn.smbms.service;


import java.util.List;

import cn.smbms.pojo.User;

public interface UserService {

  public List<User> findUsers(User user);
}

 

 

package cn.smbms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.User;

@Service("userService")
public class UserServiceImpl implements UserService {

@Autowired
@Qualifier("userMapper")
private UserMapper userMapper;
public UserMapper getUserMapper() {
  return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
  this.userMapper = userMapper;
}
@Override
public List<User> findUsers(User user) {
  // TODO Auto-generated method stub
  return userMapper.getUserList(user);//调用的dao层的方法
  }

}

 

applicationContext.xml

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:context="http://www.springframework.org/schema/context"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
10     http://www.springframework.org/schema/aop
11     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
12     http://www.springframework.org/schema/tx
13     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
14     http://www.springframework.org/schema/context
15     http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
16 
17     <!--配置数据源 -->
18     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
19         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
20         <property name="url"
21             value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&amp;characterEncoding=utf-8"></property>
22         <property name="username" value="root"></property>
23         <property name="password" value="root"></property>
24 
25     </bean>
26     <!--配置SqlSessionFactoryBean -->
27     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
28         <!--获取到你的数据源 -->
29         <property name="dataSource" ref="dataSource"></property>
30         <!--获取到mybatis的配置文件  注意这里使用的是value属性 -->
31         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
32         <!--使用下面这种方式获取sql文件  -->
33         <property name="mapperLocations">
34             <list>
35                 <value>classpath:cn/smbms/dao/**/*.xml</value>
36             </list>
37         </property> 
38     </bean>
39     
40 
41     
42     <!-- 如果在真实的项目中,映射器比较多的情况下,我们可以使用MapperScannerConfigure扫描基准包 -->
43     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
44         <property name="basePackage" value="cn.smbms.dao"></property>
45     </bean>
46     
47     <!--然后改造一下的我们的service层  -->
48     <!--扫描注解定义  -->
49     <context:component-scan base-package="cn.smbms.service"></context:component-scan>
50     
51 </beans>
复制代码

测试方法:

复制代码
 1     @Test
 2     public void testGetUserList(){
 3         
 4         User userCondition = new User();
 5         userCondition.setUserName("赵");
 6         userCondition.setUserRole(3);        
 7         //UserMapper userMapper=new UserMapperImpl();
 8         ApplicationContext ctx=new 
 9                 ClassPathXmlApplicationContext("applicationContext.xml");
10         
11         
12         //我们可以看下,容器为我们创建的bean的名称
13         for(String name:ctx.getBeanDefinitionNames()){
14             logger.info("IOC容器创建的Bean:"+name);
15         }
16         UserService tUserService=(UserService) ctx.getBean("userService");    
17         
18         
19         List<User> userList = tUserService.findUsers(userCondition);
20     
21         for(User user: userList){
22             logger.debug("testGetUserList userCode: " + user.getUserCode() + 
23                         " and userName: " + user.getUserName() + 
24                         " and userRole: " + user.getUserRole() + 
25                         " and userRoleName: " + user.getUserRoleName() +
26                         " and age: " + user.getAge() +
27                         " and address: " + user.getAddress());
28         }
29     }
30     
复制代码

运行结果:

 

 

posted on   ~码铃薯~  阅读(639)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示