新建一个mybatis HelloWorld

时间:2024-01-17 16:23:08

1、下载mybatis

https://github.com/mybatis/mybatis-3/

没有*好像打不开

下载一个最新版本,我这里下载的是mybatis-3.4.1.zip

里面有mybatis需要的jar和文档

2、新建一个java project ,新建lib目录把jar复制到lib下面

3、新建一个简单log4j配置文件log4j.properties,内容如下:

log4j.rootLogger = DEBUG,CONSOLE

log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p %x - %m%n log4j.logger.java.sql.ResultSet = DEBUG
log4j.logger.java.sql.Connection = DEBUG
log4j.logger.java.sql.Statement = DEBUG
log4j.logger.java.sql.PreparedStatement = DEBUG

4、新建一张测试表news 和一个测试的pojo

News.java内容:

package com.hy;

import java.sql.Date;

public class News {
private Integer id;
private String title;
private String content;
private String author;
private Date createdate; public News() { } public News(String title, String content, String author, Date createdate) {
super();
this.title = title;
this.content = content;
this.author = author;
this.createdate = createdate;
} public Date getCreatedate() {
return createdate;
} public void setCreatedate(Date createdate) {
this.createdate = createdate;
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
} }

5、写一个mybatis基础配置文件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">
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/hibernate" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/hy/map/News.xml"/>
</mappers>
</configuration>

文件中主要包含environments连接数据库的基本配置,mappers实体操作文件主要放sql语句

6、新建New.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.hy">
<select id="getNews" resultType="com.hy.News" parameterType="int">
select * from news where id=#{id}
</select>
</mapper>
parameterType 是指参数类型

7、写一个main test测试类
package com.hy;

import java.io.IOException;
import java.io.Reader; 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 Main { public static void main(String[] args) {
String resource = "com/hy/map/mybatis.config.xml";
Reader reader = null;
SqlSession sqlSession = null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
sqlSession = sqlSessionFactory.openSession();
News news = sqlSession.selectOne("getNews", 1);
System.out.println(news.getTitle());
} }

a、加载基本配置文件;b、获取SqlSessionFactory ;c、获取sqlsession;d、执行对应id的sql返回数据