MyBatis 增、删、改、查的基本实现
1、建数据库
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(64) DEFAULT NULL,
`userPass` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
)
2、建model类
public class User implements Serializable{
private Integer id;
private String userName;
private String userPass;
public User() {
super();
}
public User(Integer id, String userName, String userPass) {
this.id = id;
this.userName = userName;
this.userPass = userPass;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
}
1、基于xml文件
1)、mapper文件
<mapper namespace="models.User">
<insert id="insert" parameterType="models.User">
INSERT INTO User(userName,userPass) VALUES(#{userName},#{userPass})
</insert>
<select id="sel" parameterType="integer" resultType="User">
select * from User u where u.id=#{id}
</select>
<select id="selectUser" parameterType="integer" resultType="User">
select * from User u where u.id=#{id}
</select>
<select id="selectUsers" resultType="User">
select * from User u
</select>
<delete id="del" parameterType="integer">
DELETE FROM User WHERE id=#{id}
</delete>
<update id="update" parameterType="models.User">
UPDATE User u SET u.userName=#{userName} WHERE u.id=#{id}
</update>
</mapper>
2)、测试
public class SSs {
static SqlSessionFactory sqlSessionFactory;
static{
String resource = "mybaties.xml";
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
private void insert(){
SqlSession session = sqlSessionFactory.openSession();
User user =new User();
user.setUserName("adadfad");
user.setUserPass("asdfasdfasd");
session.insert("models.User.insert", user);
session.commit();
}
private void delete(){
SqlSession session = sqlSessionFactory.openSession();
User user =new User();
user.setId(5);
user.setUserName("adadfad");
user.setUserPass("asdfasdfasd");
int i = session.delete("models.User.del", 2);
session.commit();
System.out.println(i);
}
private void selectOne(){
SqlSession session = sqlSessionFactory.openSession();
User user2 = session.selectOne("models.User.sel",1);
System.out.println(user2.getUserName());
}
private void selectAll(){
SqlSession session = sqlSessionFactory.openSession();
List<User> users = session.selectList("models.User.selectUsers");
for (User user2 : users) {
System.out.println(user2.getUserName());
}
}
public void update(){
SqlSession session = sqlSessionFactory.openSession();
User user =new User();
user.setId(6);
user.setUserName("adadfadasdfasdf");
user.setUserPass("asdfasdfasd");
int i = session.update("models.User.update", user);
session.commit();
System.out.println(i);
}
public static void main(String[] args) {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
}
原文地址:http://cbg23.blog.51cto.com/7201812/1653771