1.导包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.配置xml
运行顺序→@Text(.java测试类)→ dao.java类 → XXXMapping.xml (SQL语句编写) → mybatis-config.xml(配置文件)
2.1、@Text(.java测试类)
@Test
public void testMyBatis() throws Exception{
MyBatisService myBatisService = new MyBatisService();
String s = myBatisService.myBatisTest();
System.out.println(s);
}
2.2、 dao.java类
功能:这个dao是一个中心,把所有的关联起来
public class MyBatisDao {
public Object findList() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);//加载xml文件
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//解析xml配置文件
SqlSession sqlSession = sqlSessionFactory.openSession();//打开一个session入口
try {
//方法一
MyBatis myBatis = (MyBatis) sqlSession.selectOne("mybatis.MyBatisMapping.findList", "sdfsdfsdgdfbh23423resf345retg345");//selectOne这个方法已经更新 //第一个指向xml文件还有id //第二个是传入的参数名
System.out.println(myBatis);
} finally {
sqlSession.close();
}
return null;
}
}
//方法二(推荐)
//注意:这里的MyBatisMapping.class类,是个接口
MyBatisMapping mapper = sqlSession.getMapper(MyBatisMapping.class);
Object list = mapper.findList(new HashMap() {{
this.put("myBatisId", "sdfsdfsdgdfbh23423resf345retg345");
}});
System.out.println(list);
2.3、XXXMapping.xml (SQL语句编写)
功能:SQL语句编写
<?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="mybatis.MyBatisMapping">
<resultMap type="entity.MyBatis" id="myBatis">
<id column="mybatis_Id" property="myBatisId" />
<result column="mybatis_content" property="myBatisContent" />
</resultMap>
<select id="findList" resultMap="myBatis"
parameterType="java.util.HashMap">
select
m.*
from t_mybatis m
where
m.mybatis_Id = #{myBatisId}
</select>
</mapper>
2.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/tornado_barrage?useUnicode=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mybatis/MyBatisMapping.xml"/>
</mappers>
</configuration>
3.作用域(Scope)和生命周期
3.1 每次收到的 HTTP 请求,就可以打开一个 SqlSession,返回一个响应,就关闭它。
3.2 SqlSession 非线程安全,用完关闭
疑问:java程序访问数据库,是不是相当于前端http请求后端数据呢?