zoukankan      html  css  js  c++  java
  • mybatis(1)


    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.beiluo.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="123"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/beiluo/pojo/Category.xml"/> <mapper resource="com/beiluo/pojo/Product.xml"/> </mappers> </configuration>

      

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        
        <mapper namespace="com.beiluo.pojo">
        	<select id="listCategory" resultType="Category">
        		select * from category_
        	</select>
        	
        	<insert id="addCategory" parameterType="Category">
        		insert into category_ ( name ) values (#{name})
        	</insert>
        	
        	<delete id="deleteCategory">
        		delete from category_ where id = #{id}
        	</delete>
        	
        	<select id="getCategory" parameterType="_int" resultType="Category">
        		select * from category_ where id = #{id}
        	</select>
        	
        	<update id="updateCategory" parameterType="Category">
        		update category_ set name=#{name} where id=#{id}
        	</update>
        	
        	<!-- 模糊查询 -->
        	<select id="listCategoryByName" parameterType="string" resultType="Category">
        		select * from category_ where name like concat('%',#{0},'%')
        	</select>
        	
        	<!-- 多条件查询 -->
        	<select id="listCategoryByIdAndName" parameterType="map" resultType="Category">
        		select * from category_ where id> #{id} and name like concat('%',#{name},'%') 
        	</select>
        	
        </mapper>
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

      

    package com.beiluo.test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    import java.util.Scanner;
    
    import javax.websocket.Session;
    
    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 com.beiluo.pojo.Category;
    
    
    
    public class TestCRUD {
    	 static TestCRUD  test =new TestCRUD();
    	
    	 Scanner input = new Scanner(System.in);
    	public static void main(String[] args) throws IOException {
    	
    		while(true){
    			int choice = test.showMenu();
    			if(choice == 6){
    				break;
    			}
    			test.chooseMenu(choice);
    		}		
    		
    	}
    	
    	
    	public int showMenu(){
    		System.out.println("=========欢迎使用mybatis的CRUD操作=========");
    		System.out.println("	1.添加商品");
    		System.out.println("	2.修改商品");
    		System.out.println("	3.删除商品");
    		System.out.println("	4.查询商品");
    		System.out.println("	5.所有商品");
    		System.out.println("	6.退出系统");
    		System.out.println("*********************");
    		System.out.print("请选择菜单:");
    		int choice = input.nextInt();
    		return choice;
    	}
    	
    	public boolean chooseMenu(int choice) throws IOException{
    		String resource = "mybatis-config.xml";
    		InputStream inputStream = Resources.getResourceAsStream(resource);
    		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    		SqlSession session = sessionFactory.openSession();
    		
    		boolean result = true;
    		String go_on = "Y";
    		//判断用户的输入
    		switch(choice){
    		case 1:
    			System.out.println("=========添加商品=========");
    			while(go_on.equals("Y")){
    				test.addCategory(session);
    				System.out.println("是否继续?(y/n)");
    				go_on = input.next();
    				go_on = go_on.toUpperCase();
    			}	
    			break;
    		case 2:
    			System.out.println("=========修改商品=========");
    			test.updaetCategory(session);
    			break;
    		case 3:
    			System.out.println("=========删除商品=========");
    			test.deleteCategory(session);
    			break;
    		case 4:
    			System.out.println("=========查询单个商品==========");
    		test.selectCategory(session);
    			break;
    		case 5:
    			System.out.println("==========查找商品列表=========");
    			listAll(session);
    			break;
    		case 6:
    			System.out.println("退出系统,谢谢使用");
    			result = false;
    			break;
    		default:
    			System.out.println("你输入的有误!!!");
    			break;	
    		}
    		
    		session.commit();
    		session.close();
    		return result;
    			
    	}
    	
    	
    	
    	
    	//增加
    	public void addCategory(SqlSession session){
    		Category c = new Category();
    		System.out.println("输入Category的name");
    		String name = input.next();
    		c.setName(name);
    		session.insert("addCategory",c);
    		listAll(session);
    	}
    
    	//查看所有Category
    	private static void listAll(SqlSession session){
    		List<Category> cs =  session.selectList("listCategory");
    		System.out.println("id	name");
    		for (Category c : cs) {
    			
    			System.out.println(c.getId()+"	"+c.getName());
    		}
    		
    	}
    	
    	public void deleteCategory(SqlSession session){
    		System.out.println("输入要删除的id");
    		Category c =new Category();
    		int id = input.nextInt();
    		c.setId(id);
    		session.delete("deleteCategory",c);
    		listAll(session);
    	}
    
    	public void selectCategory(SqlSession session){
    		System.out.println("请输入要查找的编号");
    		int id = input.nextInt();
    		Category c =session.selectOne("getCategory", id);
    		
    	System.out.println("查找的记录为:"+c.getId()+"	"+c.getName());
    	}
    
    	public void updaetCategory(SqlSession session){
    		System.out.println("请输入要修改的编号");
    		int id = input.nextInt();
    		Category c =session.selectOne("getCategory", id);
    		System.out.println("请输入要修改的名字");
    		String name = input.next();
    		c.setName(name);
    		session.update("updateCategory",c);
    		System.out.println("修改后的记录为:"+c.getId()+"	"+c.getName());
    //		listAll(session);
    	}
    
    	
    }
    

      

  • 相关阅读:
    char *p = "abcdefg"; p[0] = p[1]出错
    最近在 OS-10.9下配置opencv, cgal, latex, qt, pillow
    Python文件操作
    Python字典和集合
    Python目录操作
    python处理中文(待补充)
    混合高斯模型
    随机生成某些稀疏矩阵
    matlab注释
    C#中int,string,char[],char的转换(待续)
  • 原文地址:https://www.cnblogs.com/beiluoL/p/10674963.html
Copyright © 2011-2022 走看看