src/main/java
**/*.properties
**/*.xml
true
代码演示
第一步 User类
package com.kuang.pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
第二步 UserMapper接口
package com.kuang.mapper;
import com.kuang.pojo.User;
import java.util.List;
public interface UserMapper {
public List selectUser();
}
第三步 UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>select * from mybatis.user;
第四步 核心配置文件
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&characterEncoding=utf8
username=root
password=root
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>spring-dao.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”
xmlns:aop=“http://www.springframework.org/schema/aop”
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
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”
xmlns:aop=“http://www.springframework.org/schema/aop”
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
第五步 UserMapperImpl类
package com.kuang.mapper;
import com.kuang.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class UserMapperImpl implements UserMapper{
//在原来,我们所有的操作,都使用sqlSession来执行,现在都使用SqlSessionTemplate
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List selectUser() {
UserMapper mapper= sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}