第一步:新建一個工程,導入這兩個jarjava
第二步:在mysql下面新建數據庫和表,因爲是用來作學習,字段能夠簡單點mysql
這是項目結構sql
第三步:新建實體類User數據庫
package com.ll.model;apache
public class User {
private int id;
private String name;
private int age;session
public int getId() {
return id;
}mybatis
public void setId(int id) {
this.id = id;
}app
public String getName() {
return name;
}ide
public void setName(String name) {
this.name = name;
}學習
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
第四步:新建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,namespace的值習慣上設置成包名+sql映射文件名,這樣就可以保證namespace的值是惟一的
例如namespace="com.ll.model.userMapper"就是com.ll.model.(包名)+userMapper(userMapper.xml文件去除後綴)
-->
<mapper namespace="com.ll.mapping.userMapper">
<!-- 在select標籤中編寫查詢的SQL語句, 設置select標籤的id屬性爲getUser,id屬性值必須是惟一的,不可以重複
使用parameterType屬性指明查詢時使用的參數類型,resultType屬性指明查詢返回的結果集類型
resultType="com.ll.model.User"就表示將查詢結果封裝成一個User類的對象返回
User類就是users表所對應的實體類
-->
<select id="getUser" parameterType="int"
resultType="com.ll.model.User">
select * from users where id=#{id}
</select>
<insert id="addUser" parameterType="com.ll.model.User">
insert into users(name,age) values(#{name},#{age})
</insert>
<delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete>
<update id="updateUser" parameterType="com.ll.model.User">
update users set name=#{name},age=#{age} where id=#{id}
</update>
<select id="getAllUser" resultType="com.ll.model.User">
select * from users
</select>
</mapper>
第五步:新建配置文件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>
<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:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="password" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 註冊userMapper.xml文件,
userMapper.xml位於com.ll.mapping這個包下,因此resource寫成com/ll/mapping/userMapper.xml-->
<mapper resource="com/ll/mapping/userMapper.xml"/>
</mappers>
</configuration>
第六步:新建測試類
package com.ll.test;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.ll.model.User;
public class UnionTest {
public static void main(String[] args) {
//mybatis的配置文件
String resource = "conf.xml";
//使用類加載器加載mybatis的配置文件(它也加載關聯的映射文件)
InputStream is = UnionTest.class.getClassLoader().getResourceAsStream(resource);
//構建sqlSession的工廠
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//使用MyBatis提供的Resources類加載mybatis的配置文件(它也加載關聯的映射文件)
//Reader reader = Resources.getResourceAsReader(resource);
//構建sqlSession的工廠
//SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//建立能執行映射文件中sql的sqlSession
//注意這裏的true,是指自動提交,若是不寫只能手動進行提交
SqlSession session = sessionFactory.openSession(true);
/**
* 映射sql的標識字符串,
* me.gacl.mapping.userMapper是userMapper.xml文件中mapper標籤的namespace屬性的值,
* getUser是select標籤的id屬性值,經過select標籤的id屬性值就能夠找到要執行的SQL
*/
/* String statement = "com.ll.mapping.userMapper.getUser";//映射sql的標識字符串
//執行查詢返回一個惟一user對象的sql
User user = session.selectOne(statement, 1);
System.out.println(user);
*/
//獲取全部數據
String statement = "com.ll.mapping.userMapper.getAllUser";
List<User> list=new ArrayList<User>();
list=session.selectList(statement);
for(int i=0;i<list.size();i++){
User u=list.get(i);
System.out.println(u);
}
//插入數據
/* String statement = "com.ll.mapping.userMapper.addUser";//映射sql的標識字符串
User user=new User();
user.setAge(10);
user.setName("yangyang");
int retResult = session.insert(statement, user);
session.close();
System.out.println(retResult);*/
//刪除數據
/* String statement = "com.ll.mapping.userMapper.deleteUser";//映射sql的標識字符串
int retResult = session.delete(statement, 1);
session.close();
System.out.println(retResult);*/
//更新數據
/*String statement = "com.ll.mapping.userMapper.updateUser";//映射sql的標識字符串
User user=new User();
user.setAge(10);
user.setName("yangyang");
user.setId(2);
int retResult = session.update(statement,user);
session.close();
System.out.println(retResult);*/
}
}