MyBatis 配置文件优化

执行流程(原理):

读取配置文件,  创建SqlSessionFactory类得到SqlSession,执行相关操作

优化配置文件:

导入properties文件:

a:在src里面加入db.properties文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
name=root
password=admin
b:在MyBatis.cfg.xml里面添加properties标签
<properties resource="db.properties"/>
c:别名的优化
MyBatis.cfg.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指MyBatis可以配置多个environment,default指默认环境 -->
	<properties resource="db.properties"/>
	<!-- 为指定类型指定别名 使得mapper文件可以简化引用 -->
	<typeAliases>
		<typeAlias type="entity.User" alias="enus"></typeAlias>
		<!-- 为每个包下的所有类指定别名,默认别名是指定的类名  -->
		<package name="entity"/>
	</typeAliases>
	 <environments default="development">  
        <environment id="development">  
        <!-- jdbc这个配置直接使用jdbc的提交和回滚功能,依赖于从数据源获得链接来管理事务的整个生命周期 -->
        <!-- managed这个配置基本上什么都不做,从不提交或者回滚一个连接的事务,而是让容器来管理事务的生命周期 -->
            <transactionManager type="JDBC" />  
            <!-- pooled指的是连接池 -->
            <!-- 连接数据源类型:
                            unpooled:这个类型的数据源实现只是在每次需要的时候简单的打开和关闭连接 -->
            <dataSource type="POOLED">  
                <property name="driver" value="${driver}"/>  
                <property name="url" value="${url}" />  
                <property name="username" value="${name}" />  
                <property name="password" value="${password}" />  
            </dataSource>  
        </environment>  
    </environments> 
    
    <mappers>
    	<!-- mapper定义映射sql语句的文件 -->
		<!-- <mapper resource="entity/userMapper.xml"/> -->
		<mapper resource="entity/studentMapper.xml"/>
		<mapper class="dao.UserDao2"/> 
	</mappers>  
</configuration>	


userMapper.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">
  <!-- namespace命名空间,防止sql语句的id重名 -->
  <!-- paramterType指sql语句参数类型 -->
  <!-- resultType指结果类型 -->
  
<mapper namespace="entity.userMapper">
	<select id="selectAll" parameterType="Map" resultType="enus">
<!-- 		select * from user where -->
<!-- 		id=#{id} -->
	select * from user limit #{startIndex},#{pageSize}
	</select>
	<select id="getAll" resultType="enus">
		select * from user  
	</select>
	<resultMap type="User" id="enus">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="pwd" property="password"/>
	</resultMap>
	<!-- resultType返回值结果类型 -->
	<!-- <insert id="addUser" resultType="int"> -->
	<!-- 提交参数类型:paramterType="entity.User" -->
	<insert id="addUser" parameterType="enus">

		<!-- name,pwd一定是传进来参数的属性 -->
		insert into user(name,pwd,id) values(#{name},#{pwd},#{id});

	</insert>
	<select id="selectUserPwd" resultType="enus">
		select * from user where
		pwd=#{pwd}
	</select>
	<update id="updateUser" parameterType="enus">
		update user set name=#{name},pwd=#{pwd} where id=#{id}
	</update>
	<delete id="deleteUser" >
		delete from user where id=#{id}
		
	</delete>
</mapper>
	






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值