一.单独使用Mybaits
jar包
测试:
junit
数据库驱动:
mysql-connector-java
Mybatis:
mybatis
log4j:
log4j-core
log4j-api
1.创建db,properties
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/dbName?serverTimezone=UTC
database.username=root
database.password=***
2.配置mybatis.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>
<!--要注意该文件的节点顺序,否则会报错-->
<!--引入属性文件-->
<properties resource="db.properties"/>
<settings>
<!--配置日志,固定写法-->
<setting name="logImpl" value="LOG4J2"></setting>
</settings>
<!--给实体取别名-->
<typeAliases>
<package name="dao"/>
<package name="model"/>
</typeAliases>
<!--数据源配置-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</dataSource>
</environment>
</environments>
<!--引入mapple文件-->
<mappers >
<mapper resource="mapple/studentMapple.xml"/>
</mappers>
</configuration>
3.日志
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
<logger name="org.springframework" level="INFO"></logger>
<logger name="org.mybatis" level="INFO"></logger>
<root level="debug">
<appender-ref ref="console"/>
</root>
</Loggers>
</Configuration>
4.dao和model
package dao;
import model.Student;
import java.util.List;
public interface StudentMapper {
List<Student> selectAllStudent();
}
package model;
public class Student {
private int id;
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
5.mapple
<?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="dao.StudentMapper">
<select id="selectAllStudent" resultType="student">
select * from `student`
</select>
</mapper>
6.测试
import dao.StudentMapper;
import model.Student;
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 org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
public class AppTest {
SqlSessionFactory factory = null;
@Before
public void prepare() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
factory = new SqlSessionFactoryBuilder().build(inputStream);
inputStream.close();
}
@Test
public void test(){
SqlSession session= factory.openSession();
try{
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
List<Student> list = studentMapper.selectAllStudent();
Iterator<Student> iterator = list.iterator();
while(iterator.hasNext()){
Student student = iterator.next();
System.out.println(student);
}
}finally{
session.commit();
session.close();
}
}
}