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

來源:互聯網
上載者:User

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

1.jar包

  

2.mybatis-config.xm設定檔

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//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 mapperPUBLIC "-//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);            }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.