关于mybatis配置文件解耦合 user与数据库user表单关系的映射 java类型别名

jdbc.properties文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis_db
#这里username不提示,软件的问题
jdbc.username=root
jdbc.password=root
SqlMapConfig.properties核心配置文件:

---------------------------------关于user实体类与数据库user表单反射关系的配置文件---------------------------------------- UserMapper.xml配置文件: select * from user insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}) update user set sex=#{sex} where id=#{id} delete from user where id=#{id} ----------------------------------关于操作数据库的增删改查-------------------------- import cn.itcast.domain.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 org.junit.Test; import java.io.InputStream; import java.util.Date; import java.util.List; public class testFindAll { //查询所有 @Test public void TestFindAll() throws Exception{ //1.加载核心配置文件(SQLMapConfig.xml) InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.构建SQLSessionFactory工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //3.通过工厂创建SQLSession会话对象(类似Connection) SqlSession sqlSession = sqlSessionFactory.openSession(); //4.执行SQL语句 List list = sqlSession.selectList("UserMapper.findAll"); for (User user : list) { System.out.println(user); } //5.释放资源 sqlSession.close(); //is io流会默认关闭 } //新增 @Test public void testSave() throws Exception{ //1.加载核心配置文件 InputStream is = Resources.getResourceAsStream("SQLMapConfig.xml"); //2.构建SQLSessionFactory工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //3.通过工厂创建sqlSession会话对象(类似jdbc中的connection) SqlSession sqlSession = sqlSessionFactory.openSession(); //4.执行SQL语句 User user = new User(); user.setUsername("Jack"); user.setBirthday(new Date()); user.setAddress("云南"); int insert = sqlSession.insert("UserMapper.save",user); if(insert>0){ System.out.println("添加成功!"); }else{ System.out.println("添加失败!"); } //这里要注意:mybatis需要手动提交事务(DML) sqlSession.commit(); //5.释放资源 sqlSession.close(); } //修改 @Test public void testUpdate() throws Exception{ //1.加载核心配置文件 InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.构建SQLSourceFactory工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //3.通过SQLSourceFactory工厂对象构建sqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //4.执行SQL语句 User user = new User(); user.setId(49); //user.setUsername("Jack"); //user.setBirthday(new Date()); user.setSex("女"); //user.setAddress("上海"); int update = sqlSession.update("UserMapper.update",user); sqlSession.commit(); if(update>0){ System.out.println("修改成功!"); }else{ System.out.println("修改失败!"); } //5.释放资源 //Io 自动关闭 is sqlSession.close(); } //删除 @Test public void testDelete()throws Exception{ //1.加载配置文件 InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.构建SQLSessionFactory工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //3.通过SQLSessionFactory工厂对象来获取SQLSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //4.执行SQL语句 User user = new User(); user.setId(49); int delete = sqlSession.delete("UserMapper.delete", user); if(delete>0){ System.out.println("删除成功!"); }else{ System.out.println("删除成功!"); } //5.关闭资源 sqlSession.commit(); sqlSession.close(); } } -------------------------------------java类型别名------------------------------- //设置java类型别名 //设置一个java类型别名 //将整个包下所有的类名设置了别名,别名(小名):类名

你可能感兴趣的:(笔记)