<?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>