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="db.properties"></properties>
	<settings>
		<!-- 开启驼峰命名 -->
		<setting name="mapUnderscoreToCamelCase" value="true" />
	</settings>

	<!-- 配置坏境 -->
	<environments default="dev_mysql">
		<environment id="dev_mysql">
			<!-- 配置事务管理器 -->
			<transactionManager type="JDBC"></transactionManager>
			<!-- 配置数据源 -->
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driverclass}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.username}" />
				<property name="password" value="${jdbc.password}" />
			</dataSource>
		</environment>
		<environment id="dev_orcle">
			<!-- 配置事务管理器 -->
			<transactionManager type="JDBC"></transactionManager>
			<!-- 配置数据源 -->
			<dataSource type="POOLED">
				<property name="driver" value="${oracle.driverclass}" />
				<property name="url" value="${oracle.url}" />
				<property name="username" value="${oracle.username}" />
				<property name="password" value="${oracle.password}" />
			</dataSource>
		</environment>
	</environments>
	<!-- 配置连接多种数据库 -->
	<databaseIdProvider type="DB_VENDOR">
		<!-- 注意此处容易出错:原因将MySQL写成了Mysql -->
		<property name="MySQL"  value="mysql"/>
		<property name="SQL Server" value="sqlserver" />
		<property name="DB2" value="db2" />
		<property name="Oracle" value="oracle" />
	</databaseIdProvider>

	<!-- 映sql映射文件 -->
	<mappers>
		<!-- 批量注册sql映射文件 -->
		<package name="zhang.mybatis.dao"/>
	</mappers>

</configuration>

jdbc.username=root
jdbc.password=zhangyukang
jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis


#jdbc.user=scott
#jdbc.password=305316
#jdbc.driverclass=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl

<?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="zhang.mybatis.dao.CustomerMapper" >

	<select id="getCustomerById" resultType="zhang.mybatis.test.Customer" databaseId="mysql">
		select * from Customer
		where id = #{id}
	</select>
	<select id="getAllCustomer" resultType="zhang.mybatis.test.Customer" databaseId="mysql" >
		select * from Customer
	</select>
	<select id="saveCustomer" parameterType="zhang.mybatis.test.Customer" databaseId="mysql" >
		Insert into Customer(id,name,age,email) values
		(#{Customer.id},#{Customer.name},#{Customer.age},#{Customer.email})
	</select>

</mapper>

<?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="zhang.mybatis.dao.PersonMapper" >
	<select id="getPersonById" resultType="zhang.mybatis.entities.Person" databaseId="mysql" >
		select * from
		Person where
		id = #{id}
	</select>
	<select id="getPersonByName" resultType="zhang.mybatis.entities.Person" databaseId="mysql" >
		select * from Person
		where last_Name = #{lastName}
	</select>
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值