Mybatis的核心配置文件
1、Mybatis的核心配置文件层级关系
2、environments标签
数据库环境的配置,支持多环境配置
3、mapper标签
4、properties标签
在resource包下创建名为jdbc.properties的file文件
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=011012
修改sqlMapConfig.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>
<properties resource="jdbc.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<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\mapper\UserMapper.xml"></mapper>
</mappers>
</configuration>
5、typeAliases标签
现在修改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">
<mapper namespace="userMapper">
<delete id="delete" parameterType="int">
delete from where id=#{id}
</delete>
<update id="update" parameterType="com.domain.User">
update user set username=#{username},password=#{password} where id=#{id}
</update>
<insert id="save" parameterType="com.domain.User">
insert into user values(#{id},#{username},#{password})
</insert>
<select id="findAll" resultType="user">
select * from user
</select>
</mapper>
在来运行test01,发现只有int修改可以,而自定义的user则报错,原因是user没有自定义,修改sqlMapConfig.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>
<properties resource="jdbc.properties"></properties>
<typeAliases>
<typeAlias type="com.domain.User" alias="user"></typeAlias>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<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\mapper\UserMapper.xml"></mapper>
</mappers>
</configuration>
PS:注意添加的顺序 在运行test01
|