MyBatis学习:
1、导包:
mysql驱动包:
mysql-connector-java-5.1.7-bin.jar
mybatis核心包:
mybatis-3.5.1.jar
mybatis依赖包:
ant-1.10.3.jar
log4j-1.2.17.jar
ant-launcher-1.10.3.jar
commons-logging-1.2.jar
slf4j-api-1.7.26.jar
asm-7.0.jar
log4j-api-2.11.2.jar
log4j-core-2.11.2.jar
javassist-3.24.1-GA.jar
ognl-3.2.10.jar
slf4j-log4j12-1.7.26.jar
cglib-3.2.10.jar
2、添加日志配置文件(src下写配置文件:log4j.properties)
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# 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
3、创建全局配置文件SqlMapConfig.xml(src下写SqlMapConfig.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="dbconfig.properties"></properties>
<environments default="development">
<environment id="development">
<!--配置JDBC事务控制,由mybatis进行管理-->
<transactionManager type="JDBC"/>
<!--配置数据源,采用dbcp连接池-->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
</configuration>
4、添加数据库配置文件(src下写dbconfig.properties)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis_study01?useUnicode=true&characterEncoding=utf8
username=root
password=root
5、Dao层搭建(在src下com.hwt包下创建mapper包(即dao),里面写接口既实现方法)
UserMapper:
package com.hwt.dao;
import com.hwt.model.User;
import com.hwt.vo.UserQueryVo;
import java.util.List;
public interface UserMapper {
public User findUserById(int id);
public int insertUser(User user);
}
创建imp包,里面写实现UserMapper.xml:
<?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.hwt.dao.UserMapper">
<select id="findUserById" parameterType="int" resultType="user">
select * from user where id=#{id};
</select>
<insert id="insertUser" parameterType="user">
insert into user (username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address});
</insert>
</mapper>
6、配置文件加载映射文件(src下总的sql配置文件sqlMapConfig.xml中配置)
<!--告诉mybatis加载映射文件-->
<mappers>
<mapper resource="com/hwt/mapper/UserMapper.xml"></mapper>
</mappers>
7、测试
package com.hwt.test;
import com.hwt.model.User;
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.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class Demo01 {
SqlSession sqlSession = null;
@Before
public void before() throws IOException {
//读取配置文件
String resource = "SqlMapConfig.xml";
InputStream is = Resources.getResourceAsStream(resource);
//创建会话工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//打开会话
sqlSession = sessionFactory.openSession();
}
@After
public void after(){
//关闭会话
sqlSession.commit();
}
@Test
public void test01() throws IOException {
//调用方法执行数据库操作
User user = sqlSession.selectOne("findUserById", 10);
System.out.println(user);
}
}