标签:sel url stat 失败 http mes cat 注意 time
创建好了工程文件后,先将需要用到的jar包导入,创建一个mybatis-config.xml文件,源码如下:
1 <environments default="development"> 2 <environment id="development"> 3 <transactionManager type="JDBC"></transactionManager> 4 <dataSource type="POOLED"> 5 <!--数据库驱动--> 6 <property name="driver" value="com.mysql.cj.jdbc.Driver"/> 7 <!--数据库URL--> 9 <property name="url" value="jdbc:mysql://127.0.0.1:3306/**?serverTimezone=UTC"/> 10 <!--以下是数据库用户名以及密码--> 11 <property name="username" value="***"/> 12 <property name="password" value="***"/> 13 </dataSource> 14 </environment> 15 </environments> 16 17 <mappers> 18 <!--映射文件的地址--> 19 <mapper resource="com/pn/mapper/UserMapper.xml"/> 20 </mappers>
1 <!--对应的接口--> 2 <mapper namespace="com.pn.mapper.UserMapper"> 3 <!-- 语句查询结果 和 java对象的映射 --> 4 <!--用来处理查询结果和java对象的字段对应问题--> 5 6 <!-- SQL语句--> 7 <select id="selectUser" resultType="com.pn.entity.User"> 8 select * from user where userID = #{ID} 9 </select> 10 11 <select id="selectAll" resultType="com.pn.entity.User"> 12 select * from user 13 </select> 14 15 <insert id="addUser" parameterType="com.pn.entity.User"> 16 insert into user(userID,userName,userPassword) values (#{userID},#{userName},#{userPassword}) 17 </insert> 18 19 <update id="updataUser" parameterType="com.pn.entity.User"> 20 update user set userName = #{userName},userPassword = #{userPassword} where userID = #{userID} 21 </update> 22 23 <delete id="deleteUser" > 24 delete from user where userID = #{userID} 25 </delete> 26 </mapper>
因为要SqlSessionFactoryBuilder类去解析配置文件 并实例化sqlSessionFactory,通过sqlSessionFactory来获得sqlSession 对象,将其写成一个util。
1 public class mybatisUtil { 2 public static SqlSessionFactory getSqlSessionFactory(){ 3 String resource = "com/pn/resource/mybatis-config.xml"; 4 InputStream inputStream = null; 5 try { 6 //获取配置文件的流信息 7 inputStream = Resources.getResourceAsStream(resource); 8 } catch (IOException e) { 9 e.printStackTrace(); 10 } 11 //通过SqlSessionFactoryBuilder类去解析配置文件 并实例化sqlSessionFactory 12 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 13 return sqlSessionFactory; 14 } 15 16 public static SqlSession getSession(){ 17 SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); 18 return sqlSessionFactory.openSession(); 19 } 20 }
1 public class UserDao { 2 //获取全部数据 3 public List<User> getAll(){ 4 SqlSession session = mybatisUtil.getSession(); 5 List<User> userList = session.selectList("com.pn.mapper.UserMapper.selectAll"); 6 return userList; 7 } 8 //通过ID查询用户数据 9 public User getUserByID(int id){ 10 SqlSession session = mybatisUtil.getSession(); 11 User user = session.selectOne("com.pn.mapper.UserMapper.selectUser",id); 12 session.close(); 13 return user; 14 } 15 //添加用户 16 public int addUser(User user){ 17 SqlSession sqlSession = mybatisUtil.getSession(); 18 int result = sqlSession.insert("com.pn.mapper.UserMapper.addUser",user);//insert的返回的是一个int类型 19 sqlSession.commit();//更改了数据库要记得将事务提交 20 sqlSession.close(); 21 return result; 22 } 23 //更新用户信息 24 public int updataUser(User user){ 25 SqlSession sqlSession = mybatisUtil.getSession(); 26 int result = sqlSession.update("com.pn.mapper.UserMapper.updataUser",user); 27 sqlSession.commit(); 28 sqlSession.close(); 29 return result; 30 } 31 //通过ID删除对应的用户信息 32 public int deleteUser(int id){ 33 SqlSession sqlSession = mybatisUtil.getSession(); 34 int result = sqlSession.delete("com.pn.mapper.UserMapper.deleteUser",id); 35 sqlSession.commit(); 36 sqlSession.close(); 37 return result; 38 } 39 }
1 public static void main(String[] args){ 2 UserDao userDao = new UserDao(); 3 //selectAll 4 List<User> userList = userDao.getAll(); 5 for(User user:userList){ 6 System.out.println(user.getID()+" "+user.getUsername()+" "+user.getPwd()); 7 } 8 //通过ID查询用户信息 9 int id = 1500006; 10 User user = userDao.getUserByID(id); 11 System.out.println("查询结果:"+user.getID()+" "+user.getUsername()+" "+user.getPwd()); 12 //insertUser 13 User user1 =new User(); 14 user1.setID(123); 15 user1.setUsername("小明"); 16 user1.setPwd("123456"); 17 int resultInsert = userDao.addUser(user1); 18 if (resultInsert==1){ 19 System.out.println("添加成功。"); 20 }else { 21 System.out.println("添加失败。"); 22 } 23 //updateUser 24 user1.setPwd("111111"); 25 int resultUpdate = userDao.updataUser(user1); 26 if (resultUpdate==1){ 27 System.out.println("更新成功。"); 28 }else { 29 System.out.println("更新失败。"); 30 } 31 }
附上工程文件的目录如下:
在编写过程中仍有一些细节需要注意。
标签:sel url stat 失败 http mes cat 注意 time
原文地址:https://www.cnblogs.com/BleachCurtain/p/11788088.html