spring测试mybatis

public class Demo {
public static void main(String[] args) {

}
private ApplicationContext ctx;
@Before
public void init(){
ctx=new ClassPathXmlApplicationContext(new String[]{"spring/applicationContext.xml","spring/applicationContext-database.xml","SqlMapConfig.xml"});
}
@Test
public void test(){
UserMapper userMapper = ctx.getBean(UserMapper.class);
UserExample example=new UserExample();
Criteria cur = example.createCriteria();
cur.andIdEqualTo(1);
List<User> list = userMapper.selectByExample(example);
for (User user : list) {
System.out.println(user.getUsername());
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要确保在你的项目中引入了SpringMyBatis的依赖。接着,你需要进行以下步骤: 1. 配置数据源 在Spring中,你需要配置数据源。这可以通过在Spring的XML配置文件中定义一个DataSource bean来实现。以下是一个使用MySQL数据库的示例配置: ``` <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="myusername"/> <property name="password" value="mypassword"/> </bean> ``` 2. 配置MyBatis 接下来,你需要配置MyBatis。这可以通过在Spring的XML配置文件中定义一个SqlSessionFactory bean来实现。以下是一个示例配置: ``` <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> ``` 在这个配置中,我们指定了数据源和MyBatis的配置文件位置。 3. 配置Mapper 最后,你需要配置Mapper。这可以通过在Spring的XML配置文件中定义一个MapperScannerConfigurer bean来实现。以下是一个示例配置: ``` <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> ``` 在这个配置中,我们指定了Mapper接口所在的包。 完成以上步骤后,你就可以开始使用SpringMyBatis进行开发了。例如,你可以使用Spring的依赖注入功能来注入Mapper实例,并在其中使用MyBatis的方法进行数据库操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值