刚进公司实习没多久,做了一个使用struts和mybatis框架的简易个人博客平台,由于掌握的知识,能力有限平台做的非常简单,用了一些css,js想要学习的朋友可以去http://download.csdn.net/download/kereich/9942174下载,当然仅供初学者参考。这篇博客主要记录一些mybatis使用方法。
首先上一张目录图
其中blog.xml中的就是mybatis的所有xml文件了。
conf.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"></properties>
<!-- mybatis别名定义 -->
<typeAliases>
<package name="blog.bean" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<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>
<mappers>
<mapper resource="blog/xml/blog.xml" />
<mapper resource="blog/xml/comment.xml" />
<mapper resource="blog/xml/user.xml" />
</mappers>
</configuration>
blog.xml文件代码如下
<?xml version="1.0" encoding="UTF-8"?>user.xml和comment.xml代码与之类似就不贴了。在src目录下创建一个jdbc.properties文件代码:
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="blog.xml.blog">
<insert id="Add" parameterType="blog">
insert into
blog(id,type,title,article,date)
values(#{id},#{type},#{title},#{article},#{date})
</insert>
<delete id="Delete" parameterType="blog">
delete from blog where
id=#{id}
</delete>
<update id="Update" parameterType="blog">
update blog set
type=#{type},title=#{title},article=#{article},date=#{date} where
id=#{id}
</update>
<select id="List" parameterType="blog" resultType="blog">
select * from
blog order by date desc
</select>
<select id="Search" parameterType="int" resultType="blog">
select
id,type,title,article,date from blog where id=#{id}
</select>
<select id="SearchTitle" parameterType="String" resultType="blog">
select
id,type,title,article,date from blog where title=#{title}
</select>
</mapper>
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/blog
username=root
password=root
xml文件基本配置完成
创建dao层对数据库进行操作,代码如下
package blog.dao;
import java.io.Reader;
import java.util.List;
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 blog.bean.blog;
public class BlogDAO {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("blog/xml/conf.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
public void Add(blog blog) {
SqlSession session1 = sqlSessionFactory.openSession();
session1.insert("blog.xml.blog.Add", blog);
session1.commit();
session1.close();
}
public List<blog> ListAll() {
SqlSession session2 = sqlSessionFactory.openSession();
List<blog> blog1 = session2.selectList("blog.xml.blog.List");
session2.commit();
session2.close();
return blog1;
}
public void Delete(int id) {
SqlSession session3 = sqlSessionFactory.openSession();
session3.delete("blog.xml.blog.Delete", id);
session3.commit();
session3.close();
}
public blog Search(int id) {
SqlSession session4 = sqlSessionFactory.openSession();
blog blog = session4.selectOne("blog.xml.blog.Search", id);
session4.commit();
session4.close();
return blog;
}
public void Update(blog blog1) {
SqlSession session5 = sqlSessionFactory.openSession();
session5.insert("blog.xml.blog.Update", blog1);
session5.commit();
session5.close();
}
public List<blog> SearchTitle(String title) {
SqlSession session6 = sqlSessionFactory.openSession();
List<blog> blog = session6.selectList("blog.xml.blog.SearchTitle", title);
session6.commit();
session6.close();
return blog;
}
}