1.导入jar
2.spring.xml放入src
3.user 实体类
private int id;
private String username;
private String password;
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 + '\'' +
'}';
}
}
IUserDao
package com.zhongruan.dao;
import com.zhongruan.bean.User;
import java.util.List;
public interface IUserDao {
List findAll();
void delete(int id);
void update(User user);
void insert(User user);
}
UserMapper1.xml
<?xml version="1.0" encoding="UTF-8"?><select id="findAll" resultType="com.zhongruan.bean.User">
select *from tb_user;
</select>
<delete id="delete" parameterType="int">
delete from tb_user where id=#{id};
</delete>
<update id="update" 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) values(#{username},#{password});
</insert>
Test
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;
import java.util.List;
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 users=session.selectList(“findAll”);
//System.out.println(users);
// session.delete(“delete”,587);
User user1=new User();
// user.setId(5);
// user.setUsername(“aaa”);
// user.setPassword(“123”);
// session.update(“update”,user);
user1.setUsername(“hehe”);
user1.setPassword(“aaaa”);
session.insert(“insert”,user1);
session.commit();
session.close();
}
}
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>