mybatis配置文件

<?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="sql.properties"></properties>
	
	<typeAliases>
		<!-- <typeAlias type="com.mybatis.entity.Pet" alias="Pet"/> -->
		<!-- 默认别名是对应的类名 -->
		<package name="com.mybatis.entity"/>
	</typeAliases>
	<!--environments 指mybatis可以配置多个环境默认,default只想默认的环境
		每个sqlSessionFactory对应一个environment
	  -->
	<environments default="development">
		<environment id="development">
		<!-- 
			There are two TransactionManager types (i.e. type="[JDBC|MANAGED]") that are included with
		MyBatis:
			• JDBC – This configuration simply makes use of the JDBC commit and rollback facilities
			directly. It relies on the connection retrieved from the dataSource to manage the scope of the
			transaction.
			• MANAGED – This configuration simply does almost nothing. It never commits, or rolls back
			a connection. Instead, it lets the container manage the full lifecycle of the transaction (e.g.
			a JEE Application Server context). By default it does close the connection. However, some
			containers don’t expect this, and thus if you need to stop it from closing the connection, set the
			"closeConnection" property to false
		 -->
			<transactionManager type="JDBC" />
			<!-- UNPOOLED访问数据库时每次都会打开关闭连接
				 POOLED数据库池减少了每次初始化、创建连接、和请求验证的时间	
		 -->
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<!-- 定义SQL语句的映射文件 -->
		<mapper resource="com/mybatis/entity/PetMapper.xml" />
	</mappers>
</configuration>
sql.properties
driver=com.mysql.jdbc.Driver
url =jdbc:mysql://localhost:3306/database
username =root
password =xxxx
<?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)命名冲突,
 	  namespace包名+类名/包名+mapper文件名称
 	  parameterType 指sql语句参数类型
 	  resultType 指返回结果类型
 	  useGeneratedKeys 使用自增主键
  -->
<mapper namespace="com.mybatis.entity.PetMapper">
	<select id="selectPet" resultType="Pet">
		select * from pet where id = #{id}
	</select>
	
	<select id="selectAll" resultType="Pet">
		select * from pet
	</select>
	
	<insert id="insertPet" parameterType="Pet" useGeneratedKeys="true">
		insert into pet(name,owner,species,sex,birth,death) values(#{name},#{owner},#{species},#{sex},#{birth},#{death})
	</insert>
	
	<update id="updatePet" parameterType="Pet">
		update pet set name=#{name},sex=#{sex} where id=#{id}
	</update>
	
	<update id="deletePet">
		delete from pet where id=#{id}
	</update>
</mapper>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值