核心配置文件:
<?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="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatisstudy" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<!-- 找到sql语句的xml文件 -->
<mappers>
<mapper resource="com/diyun/config/sqlxml/Message.xml" />
</mappers>
</configuration>
引入:
Reader reader=Resources.getResourceAsReader("com/diyun/config/Configuration.xml");
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession=sqlSessionFactory.openSession();
<?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="Message">
<!--查询结果放入JavaBean -->
<resultMap type="com.diyun.bean.Message" id="MessageResult">
<id column="ID" jdbcType="INTEGER" property="id" />
<result column="COMMAND" jdbcType="VARCHAR" property="command" />
<result column="CONTENT" jdbcType="VARCHAR" property="content" />
<result column="DESCRIPTION" jdbcType="VARCHAR" property="description" />
</resultMap>
<!-- select语句 -->
<select id="queryMessageList" parameterType="com.diyun.bean.Message"
resultMap="MessageResult">
select * from message
<where><!-- where代替了where 1=1,并能删去多余的‘and’ -->
<if test="command!=null and command.length()>0">and COMMAND= #{command}</if>
<if test="description!=null and description.length()>0">and DESCRIPTION like "%"#{description}"%"</if>
</where>
</select>
<delete id="deleteOne" parameterType="int"> delete from message where
ID= #{id} </delete>
<delete id="deleteBatch" parameterType="java.util.List">
delete from message where ID in(
<foreach collection="list" item="item" separator=","> #{item}
</foreach>
)
</delete>
<update id="updateMessage">
update message
<set>
<if></if>
<if></if>
</set>
</update>
</mapper>
调用:
sqlSession=dbAccess.getSqlSession();
sqlSession.select("Message.selectOne",id);//只能传一个数据进去,可以是bean,第二个参数对应parameterType,返回值对应resultMap
sqlSession.commit();//提交事务
Mybatis接口式编程
sqlSession.select("Message.selectOne",id)
对于上面的代码,我们发现:
1.手写命名空间Message和select标签的id
2.第二个参数是Object类型,没有类型检查,万一与配置文件中的 parameterType属性不对应怎么办?
3.同样,返回值也没有检查,可能与resultMap不一致
于是有了Mybatis接口式编程。
先定义接口:
public interface ICommand {
public List<Command> queryCommandList();
}
此时配置文件中的namespace的id=全限定接口名:
<mapper namespace="com.diyun.dao.ICommand">
调用:
sqlSession=dbAccess.getSqlSession();
ICommand icommand=sqlSession.getMapper(ICommand.class);
list = icommand.queryCommandList();
为什么这个Icommand接口没有实现具体类,却能执行方法?
动态代理。
相当于:
Proxy.newProxyInstance(类加载器,接口,MapperProxy)<==sqlSession.getMapper(ICommand.class);
ICommand icommand=Proxy.newProxyInstance();
icommand.queryCommandList()<==MapperProxy.invoke()里面有代码:sqlSession.selectList();
最终使得:icommand.queryCommandList(param)==sqlSession.selectList(namespace.id,param);
有点乱,简单来说:
根据接口自动生成实现该接口的代理实体类MapperProxy,然后通过该实体类实现接口方法里面加上sqlSession.selectList(namespace.id,param),而且正好namespace.id就是接口的全限定~
有时间自己实现一遍。
搞定啦,
戳这里
另外:
mybatis有过滤器的,不过好复杂。。。暂且留着 -> 搞定啦
login4j的properties写好以后不需要手动读取配置,mybatis会自动发现,并打印出关键信息(尤其是会打印出执行的sql语句)
mybatis有过滤器的,不过好复杂。。。暂且留着 -> 搞定啦
login4j的properties写好以后不需要手动读取配置,mybatis会自动发现,并打印出关键信息(尤其是会打印出执行的sql语句)
补充
1.parameterType为String、Integer或者其他非集合类时,变量名用_parameter代替,否则实测会报错(尤其是做if判断时)
<if test="_parameter!=null and _parameter.length()>0">
and typename=#{_parameter}
</if>
3.又学到了关于mybatis的新东东!在config配置文件中加入:(原来mybatis中默认有列别名映射属性名的,而且还能开启方便的驼峰命名法转化)
<settings>
<!--使用jdbc的getGeneratedKeys方法获取自增主键值 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用列别名映射属性名,mybatis默认开启 -->
<setting name="useColumnLabel" value="true"/>
<!-- 开启驼峰命名法转换,Table(create_time)=>Entity(createTime) -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>