在 连接数据库的xml 文件中配置typeAliases,这样映射文件的xml的属性parameterType 就可以用其对应的alias 取代,不区分大小写
<?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>
<!--可以在标签内部配置连接数据库信,也可以通过属性引用外部配置文件的信息
resource属性: 常用的
用于指定配置文件的位置,是按照类路径的写法来写,并且必须存在于类路径下
url属性:
要求按照url用法来写
URL:Uniform Resource Locator 统一资源定位符 他是可以唯一表示一个资源的位置
他的写法:
http:localhost:8080/mybatiesserver/demoSerlet
协议 主机 端口 URI
URI:Uniform Resource Identifier 统一资源标识符。他是在应用中可以唯一定位一个资源
-->
<properties resource="jdbcConfig.properties">
</properties>
<!--使用 typeAliases配置别名,他只能配置domain中的类的别名-->
<typeAliases>
<!--typeAlias用于配置别名,type属性指定的是实体类全限定类名,alias属性指定别名,当指定了别名就不会区分大小写-->
<typeAlias type="com.domain.User" alias="user" />
</typeAliases>
<environments default="mysql">
<!--配置 MySQL 环境-->
<environment id="mysql">
<transactionManager type="JDBC"/>
<!--配置连接池-->
<dataSource type="POOLED">
<!--多了个 jdbc. 是因为配置文件里面前缀就是 jdbc. 所以要保持一致-->
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/dao/IUserMapping.xml" />
</mappers>
</configuration>
parameterType 可以用 USER,User,user,uSer取代,因为不区分大小写
<?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.dao.IUserMapping">
<select id="findAll" resultType="com.domain.User">
select *from user;
</select>
<insert id="insertUser" parameterType="User" >
<selectKey keyProperty="id" keyColumn="id" resultType="integer" order="AFTER">
select last_insert_id();
</selectKey>
insert into user(username, password, birthday, address) values(#{username},#{password},#{birthday},#{address});
</insert>
<select id="selectByIdUser" parameterType="integer" resultType="com.domain.User">
select *from user where id=#{id};
</select>
<select id="selectByNameUser" parameterType="string" resultType="com.domain.User">
select *from user where username=#{name};
</select>
<delete id="DeleteByIdUser" parameterType="integer" databaseId="int">
delete from user where id=#{userId};
</delete>
<update id="UpdateUser" parameterType="user">
update user set username=#{username},password=#{password},birthday=#{birthday},address=#{address} where id=#{id};
</update>
</mapper>
typeAlis 使用过多有点麻烦,这时就投提供了 package 标签
还有一种package是mappers的package,他的作用是指定接口
<?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>
<!--可以在标签内部配置连接数据库信,也可以通过属性引用外部配置文件的信息
resource属性: 常用的
用于指定配置文件的位置,是按照类路径的写法来写,并且必须存在于类路径下
url属性:
要求按照url用法来写
URL:Uniform Resource Locator 统一资源定位符 他是可以唯一表示一个资源的位置
他的写法:
http:localhost:8080/mybatiesserver/demoSerlet
协议 主机 端口 URI
URI:Uniform Resource Identifier 统一资源标识符。他是在应用中可以唯一定位一个资源
-->
<properties resource="jdbcConfig.properties">
</properties>
<!--配置properties-->
<!--为什么这么写?这里可支持将内容放到外部文件中-->
<!-- <properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</properties>-->
<!--使用 typeAliases配置别名,他只能配置domain中的类的别名-->
<typeAliases>
<!--typeAlias用于配置别名,type属性指定的是实体类全限定类名,alias属性指定别名,当指定了别名就不会区分大小写-->
<!-- <typeAlias type="com.domain.User" alias="user" />-->
<!--用于指定要配置别名的包,当指定后,该报下的实体类都会注册别名,并且类名就是别名,不再区分大小写-->
<package name="com.domain.User"/>
</typeAliases>
<environments default="mysql">
<!--配置 MySQL 环境-->
<environment id="mysql">
<transactionManager type="JDBC"/>
<!--配置连接池-->
<dataSource type="POOLED">
<!--多了个 jdbc. 是因为配置文件里面前缀就是 jdbc. 所以要保持一致-->
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- <mapper resource="com/dao/IUserMapping.xml" />-->
<!--package 标签适用于指定dao接口所在的包,当指定了之后就不需要再写mapper以及,resource或者class了-->
<package name="com.dao"/>
</mappers>
</configuration>
原文:https://www.cnblogs.com/zuiren/p/11406111.html