JDBC连接:
Dao包:执行sql语句,获取操作结果封装的对象,返回操作结果。
Mybatis:(向dao提供SqlSession)
(1)能与数据库交互(2)能执行sql语句
SqlSession作用:
(1)向SQL传入参数
(2)执行SQL语句
(3)获取执行SQL语句结果
(4)事物的控制
如何获得SqlSession:
(1)通过配置文件获取相关数据库连接的信息
(2)通过配置信息构建SqlSessionFactory
(3)通过SqlSessionFactory打开数据库会话
mybatis下载网址:https://github.com/mybatis/mybatis-3/releases
mybatis驱动路径:D:\JavaWeb\mybatis\源码包\mybatis-3-mybatis-3.4.2\src\test\java\org\apache\ibatis\submitted\complex_property
配置步骤:
(1)把mybatis包导入工程,放到lib下边。并创建对应的jsp页面。
(2)打开mybatis的驱动路径,复制下边的xml驱动文件,mybatis驱动路径:D:\JavaWeb\mybatis\源码包\mybatis-3-mybatis-3.4.2\src\test\java\org\apache\ibatis\submitted\complex_property。
(3)更改图上的代码为mysql,还有你的数据库,用户名和密码。
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mirco"/>
<property name="root" value="root"/>
<property name="root" value="root"/><!-- 补上用户密码-->
</dataSource>
(4)测试时候可以先注释一些代码。
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2009-2016 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--
<settings>
<setting name="useGeneratedKeys" value="false"/>
<setting name="useColumnLabel" value="true"/>
</settings>
<typeAliases>
<typeAlias alias="UserAlias" type="org.apache.ibatis.submitted.complex_property.User"/>
</typeAliases>
-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value=""/>
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mirco"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="util/sqlxml/Message.xml"/>
</mappers>
</configuration>
(5)创建Message.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2016 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 通过namespace区分,不同的namespace可以有同样的id -->
<mapper namespace="Message">
<resultMap type="bean.Message" id="MessageResult">
<!-- 主键用id,非主键用result -->
<id column="ID" jdbcType="INTEGER" property="id"/>
<result column="COMMAND" jdbcType="VARCHAR" property="command"/>
<result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/>
<result column="CONTENT" jdbcType="VARCHAR" property="content"/>
</resultMap>
<select id="queryMessage" resultMap="MessageResult">
select ID,COMMAND,DESCRIPTION,CONTENT from message where 1=1;
</select>
</mapper>
(6)创建真正和数据库操作的层,db,创建类DBAccess
package db;
import java.io.IOException;
import java.io.Reader;
import javax.annotation.Resource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/*
* 专门用来访问数据库类
* */
public class DBAccess {
//dao层处理异常
public SqlSession getSqlSession() throws IOException{
//(1)通过配置文件获取相关数据库连接的信息。不建议放到src下边,每个文件有分类去放
Reader reader=Resources.getResourceAsReader("util/Configuration.xml");
//(2)通过配置信息构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(reader);
//(3)通过SqlSessionFactory打开数据库会话
SqlSession sqlSession=sqlSessionFactory.openSession();
return sqlSession;//数据库访问层完成
}
}
(7)创建MessagqDao,通过简单的调式得出没有报错。
package dao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import db.DBAccess;
import bean.Message;
/*
* 根据查询条件查询
* */
public class MessagqDao {
public List<Message> queryMessage(String command,String description){
DBAccess dBAccess=new DBAccess();
List<Message> messageList=new ArrayList<Message>();
SqlSession sqlSession=null;//后边要关掉
try {
sqlSession=dBAccess.getSqlSession();
//sqlSession执行SQL语句
messageList =sqlSession.selectList("Message.queryMessage");//唯一的
} catch (IOException e) {
e.printStackTrace();
}
finally{
if(sqlSession!=null)
sqlSession.close();
}
return messageList;
}
public static void main(String[] args ){
MessagqDao messagqDao=new MessagqDao();
messagqDao.queryMessage("", "");
}
}