MyBatis框架基于Annotation注解的数据库增删改查操作详解编程语言

 代码:

User.java

package com.bean; 
 
import java.io.Serializable; 
 
public class User implements Serializable { 
	private Integer id; 
	private String name; 
	private String sex; 
	private Integer age; 
	public User() { 
		super(); 
		// TODO Auto-generated constructor stub 
	} 
	public User(String name, String sex, Integer age) { 
		super(); 
		this.name = name; 
		this.sex = sex; 
		this.age = age; 
	} 
	public Integer getId() { 
		return id; 
	} 
	public void setId(Integer id) { 
		this.id = id; 
	} 
	public String getName() { 
		return name; 
	} 
	public void setName(String name) { 
		this.name = name; 
	} 
	public String getSex() { 
		return sex; 
	} 
	public void setSex(String sex) { 
		this.sex = sex; 
	} 
	public Integer getAge() { 
		return age; 
	} 
	public void setAge(Integer age) { 
		this.age = age; 
	} 
	@Override 
	public String toString() { 
		// TODO Auto-generated method stub 
		return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]"; 
	} 
	 
} 

FKSqlSessionFactory.java

package com.factory; 
 
import java.io.InputStream; 
import org.apache.ibatis.io.Resources; 
import org.apache.ibatis.session.SqlSession; 
import org.apache.ibatis.session.SqlSessionFactory; 
import org.apache.ibatis.session.SqlSessionFactoryBuilder; 
 
public class FKSqlSessionFactory { 
	 
	private static SqlSessionFactory sqlSessionFactory = null; 
	 
	// 初始化创建SqlSessionFactory对象 
	static{ 
		try { 
			// 读取mybatis-config.xml文件 
			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); 
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 
		} catch (Exception e) { 
			e.printStackTrace(); 
		} 
	} 
	 
	// 获取SqlSession对象的静态方法 
	public static SqlSession getSqlSession(){ 
		return sqlSessionFactory.openSession(); 
	} 
 
	// 获取SqlSessionFactory的静态方法 
	public static SqlSessionFactory getSqlSessionFactory() { 
		return sqlSessionFactory; 
	} 
} 

UserMapper.java

package com.mapper; 
 
import java.util.List; 
 
import org.apache.ibatis.annotations.Delete; 
import org.apache.ibatis.annotations.Insert; 
import org.apache.ibatis.annotations.Options; 
import org.apache.ibatis.annotations.Param; 
import org.apache.ibatis.annotations.Result; 
import org.apache.ibatis.annotations.Results; 
import org.apache.ibatis.annotations.Select; 
import org.apache.ibatis.annotations.Update; 
 
import com.bean.User; 
 
public interface UserMapper { 
     @Insert("insert into tb_user3(name,sex,age) values(#{name},#{sex},#{age})") 
     @Options(useGeneratedKeys=true,keyProperty="id") 
     int saveUser(User user); 
     @Delete("delete from tb_user3 where id=#{id}") 
     int removeUser(@Param("id") Integer id); 
     @Update("update tb_user3 set name=#{name},sex=#{sex},age=#{age} where id=#{id}") 
     void modifyUser(User user); 
     @Select("select * from tb_user3 where id=#{id}") 
     @Results({ 
 		@Result(id=true,column="id",property="id"), 
 		@Result(column="name",property="name"), 
 		@Result(column="sex",property="sex"), 
 		@Result(column="age",property="age") 
 	}) 
     User selectUserById(@Param("id") Integer id); 
     @Select("select * from tb_user3") 
     List<User> selectAllUser(); 
} 

InsertTest.java

package com.test; 
 
import org.apache.ibatis.session.SqlSession; 
 
import com.bean.User; 
import com.factory.FKSqlSessionFactory; 
import com.mapper.UserMapper; 
 
public class InsertTest { 
	public static void main(String[] args) throws Exception { 
		// 获得Session实例 
		SqlSession session=FKSqlSessionFactory.getSqlSession(); 
		// 创建UserMapper实例 
		UserMapper um = session.getMapper(UserMapper.class); 
		// 创建User对象并设置属性 
		User user=new User(); 
		user.setName("haiges"); 
		user.setSex("男"); 
		user.setAge(18); 
		// 插入数据 
		um.saveUser(user); 
		// 查看插入数据生成的主键 
		System.out.println("插入数据生成的主键id为:" + user.getId()); 
		session.commit(); 
		session.close(); 
	} 
} 

DeleteTest.java

package com.test; 
 
import org.apache.ibatis.session.SqlSession; 
 
import com.factory.FKSqlSessionFactory; 
import com.mapper.UserMapper; 
 
public class DeleteTest { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		// 获得Session实例 
		SqlSession session = FKSqlSessionFactory.getSqlSession(); 
		UserMapper um = session.getMapper(UserMapper.class); 
		// 删除User对象 
		um.removeUser(5); 
		session.commit(); 
		session.close(); 
	} 
 
} 

UpdateTest.java

package com.test; 
 
import org.apache.ibatis.session.SqlSession; 
 
import com.bean.User; 
import com.factory.FKSqlSessionFactory; 
import com.mapper.UserMapper; 
 
public class UpdateTest { 
 
	public static void main(String[] args) throws Exception  { 
		// TODO Auto-generated method stub 
		// 获得Session实例 
		SqlSession session = FKSqlSessionFactory.getSqlSession(); 
		// 创建UserMapper实例 
		UserMapper um = session.getMapper(UserMapper.class); 
		// 查询id=5的User对象 
		User user = um.selectUserById(6); 
		//System.out.println("插入数据生成的主键id为:" + user.getId()); 
		// 修改查询到的User对象 
		user.setName("update"); 
		user.setSex("女"); 
		user.setAge(19); 
		// 修改User对象 
		um.modifyUser(user); 
		session.commit(); 
		session.close(); 
	} 
 
} 

SelectTest.java

package com.test; 
 
import org.apache.ibatis.session.SqlSession; 
 
import com.bean.User; 
import com.factory.FKSqlSessionFactory; 
import com.mapper.UserMapper; 
 
public class SelectTest { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		// 获得Session实例 
		SqlSession session = FKSqlSessionFactory.getSqlSession(); 
		// 创建UserMapper实例 
		UserMapper um = session.getMapper(UserMapper.class); 
		User user = um.selectUserById(1); 
		System.out.println(user); 
		session.commit(); 
		session.close(); 
	} 
 
} 

SelectAllTest.java

package com.test; 
 
import java.util.List; 
import org.apache.ibatis.session.SqlSession; 
import com.bean.User; 
import com.factory.FKSqlSessionFactory; 
import com.mapper.UserMapper; 
 
public class SelectAllTest { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		// 获得Session实例 
		SqlSession session = FKSqlSessionFactory.getSqlSession(); 
		// 创建UserMapper实例 
		UserMapper um = session.getMapper(UserMapper.class); 
		// 根据id查询User对象 
		List<User> list = um.selectAllUser(); 
		// 查看查询到的User对象 
		list.forEach(user->System.out.println(user)); 
		session.commit(); 
		session.close(); 
	} 
 
} 

截图(改操作):

MyBatis框架基于Annotation注解的数据库增删改查操作详解编程语言


原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/17850.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论