程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> SSM整合初級 簡單的增刪改查,ssm整合增刪

SSM整合初級 簡單的增刪改查,ssm整合增刪

編輯:JAVA綜合教程

SSM整合初級 簡單的增刪改查,ssm整合增刪


1.jar包

  

2.mybatis-config.xm配置文件

<?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>
        <!-- 按類型名定制別名  -->
        <!--   <typeAlias type="cn.happy.entity.Student" alias="Student"/> -->
        
        <!-- 拿當前指定包下的簡單類名作為別名  -->
        <package name="cn.happy.entity"/>
     </typeAliases>
 

    <environments default="mysql">
        <environment id="mysql">
            <!-- 使用jdbc的事務 -->
            <transactionManager type="JDBC" />
            <!-- 使用自帶的連接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/y2162" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="cn/happy/dao/StudentDAO.xml" />
    </mappers>
</configuration>

3.dao

public interface IStudentDAO {
    
   public int addStu(Student stu) throws IOException;
   
   //刪除
   public int delStu(int id) throws IOException;
   //查詢所有記錄
   public List<Student> findAll() throws IOException;
   
   
   //按照學生姓名查詢學生集合
   public List<Student> findStudntByName(Student stu) throws IOException;
   public List<Student> findStudntByName(String stuname) throws IOException;
   
}

daoImpl

public class StudentDAOImpl implements IStudentDAO {
    SqlSession session ;
    public StudentDAOImpl() throws IOException {
        session= MybatisUtil.getSession();
    }

    public List<Student> findStudntByName(String stuname) throws IOException {
         List<Student> list = session.selectList("findStudentByName",stuname);
         session.close();
        return list;
    }
    
     /**
      * 
      * 模糊查詢
      */
    
    public java.util.List<Student> findStudntByName(Student stu) throws IOException {
         List<Student> list = session.selectList("findStudentByName",stu);
         session.close();
        return list;
    }
    
  /**
   * 查詢所有
   */

    public java.util.List<Student> findAll() throws IOException {
         List<Student> list = session.selectList("findAll");
           session.close();
        return list;
    }
    
    /**
     * 刪除
     */
    public int delStu(int id) throws IOException {
        int result = session.delete("delStudent",id);
        session.commit();      
           session.close();
        return result;
    }
    
    
    
    public int addStu(Student stu) throws IOException {
       
       int result = session.insert("insertStudent",stu);
       session.commit();      
       session.close();
        return result;
    }

}

dao.xml

<?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="cn.happy.dao">
    <insert id="insertStudent" parameterType="Student" >
        insert into student(stuname,stuage,studate) values(#{stuname},#{stuage},#{studate})
        
     <!-- sqlserver 和Mysql 只有自自增  自增時機和insert時機: 先insert返回自增值
     
         Oracle先產生一個自增值,然後再執行insert
      -->
      <selectKey keyProperty="stuno" resultType="int">
           select @@identity
      </selectKey>

    </insert>
    <!--刪除學生  -->
    
    <delete id="delStudent">
       delete from student where stuno=#{xxx}<!-- #{xxx}隨便寫,起到一個占位的作用 -->
    </delete>
    <!-- 查詢所有 -->
    <select id="findAll" resultType="Student">
      select * from student
    </select>
    
    <!--模糊查詢  -->
    <select id="findStudentByName"  resultType="Student">
     <!--  select * from student where  stuname like  concat('%',#{stuname},'%') -->
     select * from student where  stuname like '%${value}%'
    </select>  
    
    
</mapper>

MyBatisUitl工具類

  

/**
 * 工具類
 * @author Happy
 *
 */
public class MybatisUtil {
    private static String config="mybatis-config.xml";
    static Reader reader;
    static{
        try {
            reader= Resources.getResourceAsReader(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
    //提供一個可以獲取到session的方法
    public static SqlSession getSession() throws IOException{
        
        System.out.println("22222"+factory);
        //弊病,就是工廠是
           // 1.1 openSession到底做了什麼
           SqlSession session = factory.openSession();
           System.out.println("3333");
            return session;
    }
}

單測

  

public class MyTest {
    IStudentDAO dao;
    @Before
    public void initData() throws IOException{
        dao=new StudentDAOImpl();
    }
    
    /**
     * 模糊查詢
     * @throws IOException
     */
    
    @Test
    public void findStudentByName() throws IOException{
        
        /*Student stu=new Student();
        stu.setStuname("三");*/
        List<Student> list = dao.findStudntByName("三");
        for (Student student : list) {
            System.out.println(student.getStuname());
        }
        
    }
    
    /**
     * selectALl學生
     * @throws IOException
     */
    
    @Test
    public void findAll() throws IOException{
        List<Student> list = dao.findAll();
        for (Student student : list) {
            System.out.println(student.getStuname());
        }
        
    }
    
    
    
    
    /**
     * 刪除學生
     * @throws IOException
     */
    
    @Test
    public void delStudent() throws IOException{
        dao.delStu(2);
        System.out.println("ok");
    }
    
    
    @Test
    public void testAdd() throws IOException{
        Student stu=new Student();
        stu.setStuname("張三");
        stu.setStuage(21);
        stu.setStudate(new Date());
        
        System.out.println("添加前"+stu);
        
        IStudentDAO dao=new StudentDAOImpl();
        dao.addStu(stu);
        
        System.out.println("添加後"+stu);
        
    }

}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved