Spring整合MyBatis (使用扫描包配置mapper代理)
pojo是根据表生成的实体类,属性名要跟字段名相同,不相同sql语句查询时用别名。
首先导jar包
实体类
public class User {
private Integer id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
}
第一步:
编写MyBatis配置文件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>
</configuration>
第二步:
编写Spring 配置文件applicationContext.xml
(
db.properties:文件里面放的是连接数据库的相关信息。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/t003?characterEncoding=utf-8
jdbc.username=test01
jdbc.password=
)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi<