1,新建java项目,导入jar包。
2,创建User实体com.entity.User生成set,get方法,数据库建表。
3,创建com.entity.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">
//指定唯一的命名空间namespace,一般不要.xml后缀
<mapper namespace="com.entity.UserMapper">
//id随意起,最好见名思意,parameterType为主键类型,resultType为返回类型
<select id="selectUser" parameterType="int" resultType="com.entity.User">
//sql语句
SELECT * FROM USER where id=#{id}
</select>
//配置集合
<resultMap type="com.entity.User" id="userlist">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>
<select id="selectAll" resultMap="userlist">
select * from user
</select>
<select id="delete" parameterType="int" resultMap="userlist">
delete from user where id=#{id}
</select>
<select id="add" parameterType="com.entity.User">
INSERT INTO `user`(username,password) VALUES(#{username},#{password})
</select>
<select id="update" parameterType="com.entity.User">
update set(username=#{username},password=#{password}) where id=#{id}
</select>
</mapper>
4,在src下创建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"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
//连接数据库,解决数据库乱码?characterEncoding=utf8
<property name="url" value="jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
//指定映射文件路径
<mapper resource="com/entity/UserMapper.xml"/>
</mappers>
</configuration>
5,创建测试文件Test.java
package com.util;
import java.io.InputStream;
import java.util.ArrayList;
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 com.entity.User;
public class Test {
public static void main(String[] args) throws Exception {
// 获取数据源
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(inputStream);
// 打开session
SqlSession session=factory.openSession();
//查询一条
// User user=session.selectOne("selectUser", 3);
// System.out.println(user);
List<User> list2=new ArrayList<>();
//删除
// int id=9;
// user.setId(id);
// session.delete("delete",user);
//增加
// String username="kjll";
// String password="123";
// user.setUsername(username);
// user.setPassword(password);
// session.insert("add",user);
//查询多条
List <User>list=session.selectList("selectAll");
for (User user : list) {
System.out.println(user);
}
}
}