一、搭建环境
1、创建一个Java或者web工程,导入相应jar包并为该工程创建Spring配置文件application.xml:
2、在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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 扫描其base-package标签属性值指定的包内的类、实例化被@Component、@Repository、@Service或@Controller等类注解所注解的类以及为这些类中有@Autowired、 @Resource 或@Inject注解的属性赋值。 -->
<context:component-scan base-package="com.jd"></context:component-scan>
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" lazy-init="false" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 创建SqlSessionFactory对象,将Spring和MyBatis整合,不需要MyBatis的配置映射文件 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean"
<!-- 指定数据源,注意:dataSource属性必须配置,即MyBatis和Spring整合后,将不再使用mybatis-config.xml全局配置文件中数据库配置 -->
p:dataSource-ref="dataSource"
<!-- 指定全局配置文件位置 -->
p:configLocation="classpath:mybatis-config.xml"
<!-- 指定XML映射文件位置,此时可以将mybatis_config.xml文件mappers标签删掉 -->
p:mapperLocations="classpath:./sql/*.xml">
</bean>
<!-- 扫描指定包及其子包下的mapper接口并生成响应代理对象,该标签只对接口有用 -->
<mybatis-spring:scan base-package="com.jd.*.dao" />
</beans>
二、测试
1、编写IUserInfoDao.java接口:
package com.jd.userInfo.dao;
import org.apache.ibatis.annotations.Param;
public interface IUserInfoDao {
boolean delete(@Param("ids")int [] ids);
}
2、编写全局配置文件mybatis-config.xml:
<?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>
<settings>
<!-- 是否开启自动驼峰命名规则(camel case)映射,value="true"开启 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<!-- 进行名称缩写 -->
<typeAlias type="com.jd.vo.UserInfo" alias="UserInfo"/>
</typeAliases>
</configuration>
3、编写XML映射文件user_info.xml:
<?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.jd.userInfo.dao.IUserInfoDao">
<!--
collection:指定遍历的集合名:可以用注解@param("")决定所封装的别名
item:当前遍历的对象名
open:开始的符号
close:结束的符号
separator:遍历元素间的分隔符
-->
<delete id="delete">
delete from user_info where id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>
4、编写测试类Test.java:
package com.jd.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jd.userInfo.dao.IUserInfoDao;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
IUserInfoDao userInfoDao = applicationContext.getBean(IUserInfoDao.class);
System.out.println(userInfoDao.delete(new int[] {1,8}));
applicationContext.close();
}
}