myVBatis笔记
mybatis.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>
<!-- 加载类路径下的属性文件 -->
<properties resource="db.properties"/>
<!-- 设置类型别名 -->
<typeAliases>
<typeAlias type="com.mybatis.Student" alias="student"/>
</typeAliases>
<!-- 设置一个默认的连接环境信息 -->
<environments default="mysql_developer">
<!-- 连接环境信息,取一个任意唯一的名字 -->
<environment id="mysql_developer">
<!-- mybatis使用jdbc事务管理方式 -->
<transactionManager type="jdbc"/>
<!-- mybatis使用连接池方式来获取连接 -->
<dataSource type="pooled">
<!-- 配置与数据库交互的4个必要属性 -->
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
<!-- 加载映射文件-->
<mappers>
<mapper resource="com/mybatis/StudentMapper.xml"/>
</mappers>
</configuration>
src添加db.properties文件
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mybatis
mysql.username=root
mysql.password=123456
mapper.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.qbd.ssm.dao.UserDao">
<!-- 定义缓存 一般是一级缓存,如果用同一个sqlsession 那么相同查询直接会从缓存中查找
<cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false"></cache>
-->
<!-- 查找所有 -->
<select id="find" parameterType="Map" resultMap="StudentResult">
select * from user
<where>
<if test="uname!=null and uname!='' ">
and uname like #{uname}
</if>
</where>
<if test="start!=null and size!=null">
limit #{start},#{size}
</if>
</select>
<select id="getTotal" parameterType="Map" resultType="Long">
select count(*) from user
<where>
<if test="uname!=null and uname!='' ">
and uname like #{uname}
</if>
</where>
</select>
<!-- 按照用户名和密码查找 -->
<select id="getUser" resultMap="StudentResult" parameterType="Map">
select *from user where uname=#{uname} and upassword=#{upassword}
</select>
<!-- 删除 -->
<delete id="delete" parameterType="Map">
delete from user where uid=#{uid}
</delete>
<!-- 修改 -->
<update id="update" parameterType="User">
update user
<set>
<if test="uname!=null">
uname=#{uname},
</if>
<if test="upassword!=null">
upassword=#{upassword},
</if>
<if test="upower!=null">
upower=#{upower},
</if>
</set>
where uid=#{uid}
</update>
<!-- 增加 -->
<insert id="add" parameterType="User">
insert into user values(null,#{uname},#{upassword},#{upower})
</insert>
<resultMap type="User" id="StudentResult">
<id property="uid" column="uid"/>
<result property="uname" column="uname"/>
<result property="upassword" column="upassword"/>
</resultMap>
</mapper>