Spring、Spring MVC、MyBatis框架2
原创
©著作权归作者所有:来自51CTO博客作者Java小白加油的原创作品,请联系作者获取转载授权,否则将追究法律责任
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>
</configuration>
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>
<!-- 别名 -->
<typeAliases>
<package name="com.hazy.model"/>
</typeAliases>
</configuration>
mapper 配置文件(例如 UserDaoMapper)
<?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.hazy.dao.UserDao">
<resultMap type="User" id="user">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>
<!-- id对应方法名 parameterType参数类型 resultMap结果集 -->
<!-- 查 -->
<select id="findUserAll" resultMap="user">
SELECT * FROM user
</select>
<!-- 查 -->
<select id="getUserById" parameterType="java.lang.Integer" resultMap="user">
SELECT * FROM user WHERE id=#{value}
</select>
<!-- 增 -->
<insert id="addUser" parameterType="user">
INSERT INTO user(username,password) VALUES(#{username},#{password})
</insert>
<!-- 删 -->
<delete id="delUser" parameterType="java.lang.Integer">
DELETE FROM user WHERE id=#{value}
</delete>
<!-- 改 -->
<update id="updateUser" parameterType="user">
UPDATE user SET username=#{username} WHERE id=#{id}
</update>
</mapper>
Spring头文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>