Maven+ Mybatis 入门demo
1.demo架构
2.引入pom文件
< dependencies>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.0</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context</ artifactId>
< version> 5.1.8.RELEASE</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.47</ version>
</ dependency>
</ dependencies>
3.编写jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
4.编写mybatis-config.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 = " jdbc.properties" />
< settings>
< setting name = " logImpl" value = " STDOUT_LOGGING" />
</ settings>
< typeAliases>
< package name = " com.pzy.entity" />
</ typeAliases>
< 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>
< package name = " com.pzy.dao" />
</ mappers>
</ configuration>
5.编写工具类
package com. pzy. resource;
import org. apache. ibatis. io. Resources;
import org. apache. ibatis. session. SqlSessionFactory;
import org. apache. ibatis. session. SqlSessionFactoryBuilder;
import java. io. IOException;
import java. io. InputStream;
public class SqlSessionFactoryUtils
{
private static SqlSessionFactory sqlSessionFactory;
static {
try {
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder ( ) ;
InputStream in = Resources. getResourceAsStream ( "mybatis-config.xml" ) ;
sqlSessionFactory = builder. build ( in) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
public static SqlSessionFactory getSqlSessionFactory ( ) {
return sqlSessionFactory;
}
}
6. 实体类
public class Teacher {
private Integer id;
private String name;
private Integer age;
}
dao层
package com. pzy. dao;
import com. pzy. entity. Teacher;
import org. apache. ibatis. annotations. Param;
import org. apache. ibatis. annotations. Select;
public interface TeacherMapper {
Teacher queryTeacher ( int id) ;
}
7.创建同名Mapper.xml (com/pzy/dao)
<?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.pzy.dao.TeacherMapper" >
< select id = " queryTeacher" resultType = " Teacher" >
select * from t_teacher where id = #{id}
</ select>
</ mapper>
8.测试类
import com. pzy. entity. Teacher;
import com. pzy. resource. SqlSessionFactoryUtils;
import org. apache. ibatis. session. SqlSession;
import org. apache. ibatis. session. SqlSessionFactory;
public class TeacherTest {
public static void main ( String[ ] args)
{
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils. getSqlSessionFactory ( ) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( ) ;
TeacherMapper mapper = sqlSession. getMapper ( TeacherMapper. class ) ;
Teacher teacher = mapper. queryTeacher ( 1 ) ;
System. out. println ( teacher) ;
sqlSession. close ( ) ;
}
}