22、ssm整合回顾 -mybatis层
用resultmap是因为字段名没有对应数据库字段名!
1、applicationContext.xml
<?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">
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
<import resource="spring-dao.xml"/>
</beans>
2、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.kuang.pojo"/>
</typeAliases>
<mappers>
<mapper class="com.kuang.dao.BookMapper"/>
</mappers>
</configuration>
3、database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
4、BookMapper
public interface BookMapper {
// 增加一本书
int addBook(Books books);
// 删除一本书
int deleteBookById(@Param("bookId") int id);
// 改一本书
int updateBook(Books books);
// 查询一本书
Books queryBookById(@Param("bookId") int id);
// 查询全部书
List<Books> queryAllBook();
// 查询书名
List<Books> queryBookName(@Param("bookName") String bookName);
}
5、BookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper>
<!-- values #{bookName} 前面没有等号,因为上面已经写好id,name 等参数了-->
<insert id="addBook" parameterType="Books">
insert into ssmbuild.books(bookName, bookCounts, detail)
values (#{bookName},#{booCounts},#{detail})
</insert>
<!-- bookID=#{bookId} 括号没有括起来看看可不可以 -->
<delete id="deleteBookById" parameterType="int">
delete from ssmbuild.books where bookID=#{bookId};
</delete>
<!-- 这个全部都忘了-->
<update id="updateBook" parameterType="Books">
update ssmbuild.books
set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookID =#{bookID};
</update>
<!-- resultType="Books" 注意是返回类型 -->
<select id="queryBookById" resultType="Books">
select * from ssmbuild.books where bookID=#{bookID};
</select>
<select id="queryAllBook" resultType="Books">
select * from ssmbuild.books;
</select>
</mapper>