myBatis笔记

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值