mybatis入门---helloworld程序开发步骤
1.新建一个Java Project,命名为mybatis-01-helloworld
2.工程名右键,新建一个Source Folder,名为lib
3.添加Jar包,将 mybatis-3.3.1.jar和mysql-connector-java-5.03-bin.jar包复制到lib目录下,选中两个jar包,
右键Build Path-->Add to build path,会发现工程下面多了一个Reference Liberies目录,下面放着这两个jar包,
表示java已经可以识别这两个jar包
ps:mybatis-3.3.1.jar---添加mybatis支持
mysql-connector-java-5.03-bin.jar---添加sql支持,因为mybatis是半自动的,sql语句全部自己手写
而不是像hibernate一样,完全自动生成。(hibernate是全自动的)
4.在src目录下新建mybatis的配置文件,命名为mybatis.xml(内容可以上网拷贝)
<pre name="code" class="html"><?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="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment></environments>
<mappers>
<mapper resource="org/mybatis/example/BologMapper.xml" />
</mappers>
</configuration>
然后根据自己的项目,进行相应的更改
<?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/test" /> //数据库名称
<property name="username" value="root" /> //数据库的用户名
<property name="password" value="root" /> //数据库的密码
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/etc/mapper/UserMapper.xml" />
</mappers>
</configuration>
5.在src目录下新建com.etc.mapper包,在包下新建UserMapper.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.etc.mapper.UserMapper">
<select id="findById" resultType="com.etc.mapper.User">
select user_id id ,user_name username ,user_pass password from t_user where user_id = 1
</select>
</mapper>
6.在com.etc.mapper包下新建User.java
package com.etc.mapper;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + "]";
}
}
7.在com.etc.mapper包下新建UserMapper.java文件
package com.etc.mapper;
public interface UserMapper {
User findById();
}
8.右键工程名,新建SourceFolder文件夹,新建UserMapperTest.java
package com.etc.mapper;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
public class UserMapperTest {
@Test
public void test() {
InputStream config = this.getClass().getResourceAsStream("/mybatis.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder()
.build(config);
SqlSession session = factory.openSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = userMapper.findById();
System.out.println(user);
}
}
9.右键运行,JUnit Test,控制台返回结果如下:
User [id=1, username=zoe, password=123]
10.目录架构图如下: