spring-mybatis
mybatis的3.4.0及以上版本只支持mybatis-spring1.3.0及以上版本
出现Java.lang.NoClassDefFoundError: org/apache/ibatis/cursor/Cursor 错误!
说明是版本兼容问题,解决办法:mybatis的3.4.0及以上版本用mybatis-spring1.3.0及以上版本;mybatis的3.4.0以下版本用mybatis-spring1.3.0以下版本。
mybatis 独立使用
SqlMapConfig.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>
<environments default="environment">
<environment id="environment">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/wjh" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/user.xml" />
</mappers>
</configuration>
user_mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="mapper.UserMapper">
<select id="query" resultType="entity.User">
select * from wjh_user
</select>
<select id="getUser" parameterType="int" resultType="entity.User">
select * from user where user_id=#{id}
</select>
</mapper>
>parameterType可不写
mapper 接口:
public interface UserMapper {
public List<User> query();
public User getUser(int id);
}
代码如下:
try{
String config = "SqlMapConfig.xml"; //class的根目录
Reader reader = Resources.getResourceAsReader(config);
SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sfb.build(reader);
SqlSession session = sf.openSession();
//List<User> list = session.selectList("query");
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> list = mapper.query();
session.close();
return list;
}catch (Exception e) {
Logger logger = Logger.getLogger(this.getClass());
logger.warn("报错",e);
}
return null;
和spring的整合
spring-mvc.xml
<bean id="c3p0DataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 不注册jmx -->
<constructor-arg type="boolean" value="false" />
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/wjh"/>
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
<!-- mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="c3p0DataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 别名包 -->
<property name="typeAliasesPackage" value="cn.tarena.ht.pojo"/>
</bean>
<!-- 手动加载mapper
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean> -->
<!-- 自动扫描注解的mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
<property name="annotationClass" value="annotation.MyBatisRepository"/>
</bean>
<!-- 配置sqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
补充:SqlSessionTemplate 类 销毁时会默认调用close(不管你是否设置了销毁方法为close),但是这个对象是代理对象,每执行的一个方法会获取一个SqlSession对象,执行完后关闭SqlSession对象,不需要close,调用close会报错,于是我们把销毁方法给转移到clearCache上
定义MyBatisRepository注解:
public @interface MyBatisRepository {
String value() default "";
}
定义一个Mapper接口,给上一个自己定义的注解,MyBatis会自动扫描这个接口
@MyBatisRepository
public interface UserMapper {
public List<User> query();
public User getUser(int id);
}
在mybatis-config.xml文件中
<configuration>
<!--别名配置,可以省略包名 -->
<typeAliases>
<package name="entity"/>
</typeAliases>
</configuration>
typeAliases这个标签是配置别名的,可以用package这个属性,给所有的类默认加个包名,在mapper.xml文件中可以这样写:
<mapper namespace="mapper.UserMapper">
<select id="query" resultType="User">
select * from user
</select>
<select id="getUser" parameterType="int" resultType="User">
select * from user where user_id=#{id}
</select>
</mapper>
在dao中,代码如下:
@Resource
private UserMapper userMapper;
@Resource
private SqlSessionTemplate sqlSessionTemplate;
public List<User> query2() {
return userMapper.query();
}
public List<User> query3() {
return sqlSessionTemplate.selectList("query");
}
public User getUserById(int id){
return userMapper.getUser(id);
}
void updateState( @Param("deptIds") String[] deptIds, @Param("state") int state);
- 最好不要没有set函数,自己直接注入到属性中,这样会导致以后再配置文件上不能进行配置,配置文件依赖set函数,没有set函数是不标准的javaBean(可以不写set函数,汇通项目中没有set)
- @Param(“deptIds”),把属性值传递到xml文件中,不可以省略