ITeye博客

mybatis查询入门(log4j控制台sql语句日记输出的)

wangxuehui · 2012-08-19 09:25 · mybatis · 编程语言 · log4j · Markdown

[color=red]先看demo的结构,可以先讲数据库建立起来,下面有代码里面有数据库sql语句数据库建模好之后,只要运行junit下面的test目录下面的相应方法,注意去掉ignore[/color] [img]http://dl.iteye.com/upload/attachment/0072/5280/3d599812-0b4e-32fe-921e-16dd6349ec7d.bmp[/img] [color=red]数据库的模型图[/color] [img]http://dl.iteye.com/upload/attachment/0072/5286/dcc188ba-17c6-3a18-a4f5-d658d0f21626.bmp[/img] [code="java"]import java.io.FileNotFoundException; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; /** * MyBatis Connection Factory, which reads the configuration data from a XML file. * static只读取一次xml配置文件 * @author Loiane Groner * http://loianegroner.com (English) * http://loiane.com (Portuguese) */ public class MyBatisConnectionFactory { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "SqlMapConfig.xml"; Reader reader = Resources.getResourceAsReader(resource); if (sqlSessionFactory == null) { sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } } catch (FileNotFoundException fileNotFoundException) { fileNotFoundException.printStackTrace(); } catch (IOException iOException) { iOException.printStackTrace(); } } public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } }[/code] [code="java"] [/code][code="java"] SELECT id, title, author FROM article WHERE id_status = 1 AND title LIKE #{title} SELECT id, title, author FROM article WHERE id_status = 1 AND title LIKE #{title} AND author LIKE #{author} SELECT id, title, author FROM article WHERE id_status = 1 AND title LIKE #{title} AND author LIKE #{author} AND id_category = 3 SELECT id, title, author FROM article title LIKE #{title} OR author LIKE #{author} SELECT id, title, author FROM article title LIKE #{title} AND author LIKE #{author} SELECT id, title, author FROM article WHERE id_category IN #{category} [/code]

评论(2)

  1. #2 楼 · wangxuehui · 2012-12-28 22:37

    什么意思??

  2. #1 楼 · 青春的、脚步 · 2012-12-28 10:27

评论功能维护中,当前仅展示历史评论。