SSM整合初级 简单的增删改查

Posted 董泽文

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        
    }

}

 

以上是关于SSM整合初级 简单的增删改查的主要内容,如果未能解决你的问题,请参考以下文章

[java]整合SSM框架实现简单的增删改查

ssm整合实现简单的增删改查

IDEA+Maven+SSM框架实现的简单的增删改查

IDEA版(适合新手小白) Maven整合SSM实现论文管理系统的增删改查

SSM框架整合---完成对图书数据表的增删改查

ssm表格形式的增删改查点击修改按钮获取修改时间