一、目录结构
classes是项目的根路径,编译后会将com包及其子包,resources的子包放放到classes中,其中需要注意的是java目录下的xml不会被编译到classes中,所以要在resources中建一个结构相同的mapper包 。
二、在pom文件中导入依赖的坐标
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
三、实体类
public class Website {
private int id;
private String name;
private String url;
private int age;
private String country;
private Date createtime;
}
四、mapper接口
public interface WebsiteMapper {
int addWebsite(Website website);
Website selectAllWebsite();
}
五、映射文件
和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="com.cailibin.mapper.WebsiteMapper">
<insert id="addWebsite" parameterType="com.cailibin.pojo.Website">
insert into website
(name,url,age,country)
values(#{name},#{url},#{age},#{country})
</insert>
<select id="selectAllWebsite" resultType="com.cailibin.pojo.Website">
select * from website
</select>
</mapper>
六、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>
<properties resource="dbconfig.properties"/>
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/cailibin/mapper/WebsiteMapper.xml" />
</mappers>
</configuration>
七、dbconfig.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?&useSSL=false&serverTimezone=UTC
jdbc.password=root
jdbc.username=root
八、dao
public class WebsiteDao {
private SqlSessionFactory sqlSessionFactory;
public WebsiteDao(String configPath) throws IOException {
InputStream inputStream = Resources.getResourceAsStream(configPath);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public List<Website> findAll() {
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Website> studentList = sqlSession.selectList("com.cailibin.mapper" +
".WebsiteMapper." +
"selectAllWebsite");
sqlSession.close();
return studentList;
}
public int addWebsite(Website website) {
SqlSession sqlSession = sqlSessionFactory.openSession();
int rowsAffected = sqlSession.insert("com.cailibin.mapper.WebsiteMapper" +
".addWebsite", website);
sqlSession.commit();
sqlSession.close();
return rowsAffected;
}
}
九、logej.properties
# Global logging configuration
log4j.rootLogger=ERROR,stdout
# MyBatis logging configuration...
log4j.logger.com.cailibin=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
十、测试
public class AppTest {
private WebsiteDao websiteDao;
@Before
public void init() throws IOException {
websiteDao = new WebsiteDao("mybatis-config.xml");
}
@Test
public void insertTest() {
Website website = new Website();
website.setName("编程帮");
website.setUrl("https://www.biancheng.net/");
website.setAge(21);
website.setCountry("CN");
websiteDao.addWebsite(website);
}
@Test
public void findAllTest() {
List<Website> all = websiteDao.findAll();
all.forEach(System.out::println);
}
}
|