本文为自我学习使用,如转载需本人同意
1.表现层:页面展示
2.业务层:业务流程
3.持久层:持久化数据
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="sqlConfig.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8"/>
<property name="username" value="${database.username}"/>
<property name="password" value="1234"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="CountryMapper.xml"/>
mappers>
configuration>
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="user">
<select id="selectAll" resultType="Country">
select * from sup_country
select>
mapper>
database.driver=com.mysql.cj.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8
database.username=root
database.password=1234
properties文件里不要加引号
package entity;
import java.time.LocalDateTime;
public class Country {
private Integer id;
private String countryName;
private String englishName;
private String creator;
private String modifier;
private LocalDateTime createTime;
private LocalDateTime updateTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getEnglishName() {
return englishName;
}
public void setEnglishName(String englishName) {
this.englishName = englishName;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getModifier() {
return modifier;
}
public void setModifier(String modifier) {
this.modifier = modifier;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime create_time) {
this.createTime = create_time;
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "com.entity.Country{" +
"id=" + id +
", countryName='" + countryName + '\'' +
", englishName='" + englishName + '\'' +
", creatot='" + creator + '\'' +
", modifier='" + modifier + '\'' +
", create_time=" + createTime +
", updateTime=" + updateTime +
'}';
}
}
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
/**
* Mybatis 快速入门
*/
public class app {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
System.out.println(sqlSession.selectList("user.selectAll"));
}
}
修改了下结构
注意:在resource文件中新建包不能使用点.,必须使用/
namespace为mapper接口文件
<?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="mapper.CountryMapper">
<!--设置语句-->
<select id="selectAll" resultType="Country">
select * from sup_country
</select>
</mapper>
方法名就是sql映射文件中的sql语句id,并保持参数类型和返回值类型一致
package mapper;
import entity.Country;
import java.util.List;
public interface CountryMapper {
List<Country> selectAll();
}
扫描包mapper,这能获取mapper包下的所有xml文件(不会扫描mapper接口,mapper接口是在namespace="mapper.CountryMapper"加载的),可以简化代码。
不能重复扫描,比如我们把上面图片代码的注释解除,那就会重复扫描CountryMapper.xml。这是不被允许的: