Java-Mybatis核心配置
Java-Mybatis核心配置
mybatis-config.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"/> <!-- 设置 --> <settings> <!-- 开启日志:STDOUT_LOGGING | LOG4J --> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!-- 类型别名 --> <typeAliases> <!--<typeAlias type="cn.cnyasin.pojo.User" alias="User"/>--> <package name="cn.cnyasin.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <!-- 每一个Mapper.xml都要在Mybatis核心配置文件中注册! --> <mapper resource="cn/cnyasin/dao/UserMapper.xml"/> <!--<mapper class="cn.cnyasin.dao.UserMapper"/>--><!-- 此写法要求mapper和mapper.xml必须同名且在一个目录 --> <!--<mapper url="file:///var/mappers/UserPapper.xml"/>--><!-- 不推荐 --> </mappers> </configuration>
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"> <!-- 一个namespace对应一个Mapper类(原来的dao) --> <mapper namespace="cn.cnyasin.dao.UserMapper"> <!-- id:一个id对应一个方法 resultType:返回一个结果 resultMap:返回多个结果 --> <select id="getOne" resultType="cn.cnyasin.pojo.User"> select * from user where id = #{id} </select> <insert id="addOne" parameterType="cn.cnyasin.pojo.User"> insert into user (`username`,`email`,`password`,`created_at`) values (#{username}, #{email}, #{password}, #{created_at}) </insert> <update id="updateOne" parameterType="cn.cnyasin.pojo.User"> update user set balance = #{balance} where id = #{id}; </update> <delete id="deleteOne"> delete from user where id = #{id} </delete> <resultMap id="userListMap" type="cn.cnyasin.pojo.User"/> </mapper>