1.New一个mybatis
2.在 web下新建一个lib包,导入所需的架包。
3.在src下创建所需的包
4.在已创建的包bean下,创建User.java
package com.zhongruan.bean;
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 + '\'' +
'}';
}
}
5.在dao包里,新建一个接口IUserDao
package com.zhongruan.dao;
import com.zhongruan.bean.User;
import java.util.List;
public interface IUserDao {
List<User> findAll();
void deleteById(int id);
void updateById(User user);
void insert(User user);
}
6.新建一个名为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.zhongruan.dao.IUserDao">
<select id="findAll" resultType="com.zhongruan.bean.User">
select * from tb_user
</select>
<delete id="deleteById" parameterType="int">
delete from tb_user where id=#{id}
</delete>
<update id="updateById" parameterType="com.zhongruan.bean.User">
update tb_user set username=#{username},password=#{password} where id=#{id}
</update>
<insert id="insert" parameterType="com.zhongruan.bean.User">
insert into tb_user(username,password) value (#{username},#{password})
</insert>
</mapper>
7.在test包下新建Test.java(这个只是个测试代码)
package com.zhongruan.test;
import com.zhongruan.bean.User;
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 java.io.IOException;
import java.io.Reader;
public class Test {
public static void main(String[] args) throws IOException {
Reader reader= Resources.getResourceAsReader("sqlMapConfig.xml");
SqlSessionFactory build=new SqlSessionFactoryBuilder().build(reader);
SqlSession session=build.openSession();
/*List<User>users=session.selectList("findAll");
System.out.println(users);
session.delete("deleteById",4);
session.commit();
session.close();
User user=new User();
user.setId(1);
user.setUsername("ss");
user.setPassword("2");
session.update("updateById",user);
session.commit();
session.close();*/
User user=new User();
user.setUsername("lb");
user.setPassword("21");
session.insert("insert",user);
session.commit();
session.close();
}
}
8.在src下创建名为sqlMapConfig.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>
<property name="url" value="jdbc:mysql://localhost:3306/zjgm?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zhongruan/dao/UserMapper.xml"></mapper>
</mappers>
</configuration>