版权声明:转载请指明出处https://blog.csdn.net/weixin_42321963/article/details/82937281

主要是各种配置文件,建议把整个项目搬到自己电脑上慢慢看。

 

创建maven项目

首先是各种配置文件:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zit</groupId>
    <artifactId>mybatis04</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>mybatis04</name>

    <dependencies>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>


        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>


    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

src/main/resources目录下的内容:

resources/conf.xml:

<?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>
    <!--加载db.peoperties-->
    <properties resource="db.properties"/>
    <settings>
        <setting name="logPrefix" value="dao."/>
    </settings>

    <typeAliases>
        <!--    <typeAlias type="com.zit.entity.Book" alias="bk"/>-->
        <package name="com.zit.entity"/>
    </typeAliases>


    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.username}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.zit.mapper"/>
    </mappers>

</configuration>

resources/db.properties

db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/dt_game?serverTimezone=PRC&useSSL=false&useUnicode=true&characterEncoding=utf8
db.username=root
db.password=

resources/logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %p %c [LineNumber:%L]: %m%n
            </pattern>
        </encoder>
    </appender>
    <appender name="debugLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>c:/demo.log</file>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %p %c [行号:%L]: %m%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/file/path/example/file-name.debug.log.%d{yyyy-MM-dd}
            </fileNamePattern>
        </rollingPolicy>
    </appender>
    <appender name="errorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/file/path/example/file-name.error.log</file>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %p %c [LineNumber:%L]: %m%n
            </pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/file/path/example/file-name.error.log.%d{yyyy-MM-dd}
            </fileNamePattern>
        </rollingPolicy>
    </appender>

    <root level="info">
        <appender-ref ref="console"/>
        <appender-ref ref="debugLog"/>
        <appender-ref ref="errorLog"/>
    </root>

    <logger level="debugLog" name="com.Demo"/>
    <logger level="debug" name="dao"/>
    <logger level="debug" name="com.zit.servlet.Show"/>

    <logger name="org.springframework.jdbc.core.JdbcTemplate" level="ERROR"/>
</configuration>

src/main/webapp/WEB-INF/web.xml文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
    <display-name>mybatis04</display-name>
    <!--配置web项目的web.xml文件的首页-->
    <welcome-file-list>
        <welcome-file>/show</welcome-file>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

 

src/java/com/zit目录下:

com.zit.dao.BookDao.java 和MybatisHelper.java

package com.zit.dao;

import com.zit.entity.Book;
import com.zit.mapper.BookMapper;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


public class BookDAO implements BookMapper {
    private SqlSession session;
    private BookMapper bm;

    public BookDAO() {
        this.session = new MybatisHelper().getSqlSession();
        this.bm = session.getMapper(BookMapper.class);
    }

    public List<Book> query() {
        return bm.query();
    }

    public int save(Book book) {
        int i = bm.save(book);
        session.commit();
        return i;
    }

    public int deleteById(int id) {
        int i = bm.deleteById(id);
        session.commit();
        return i;
    }

    public int updateById(Book book) {
        int i = bm.updateById(book);
        session.commit();
        return i;
    }

    public int count() {
        int i = bm.count();
        return i;
    }

    public List<String> queryNames() {
        return bm.queryNames();
    }

    public List<Map<String, Object>> queryNameAndPrice() {
        return bm.queryNameAndPrice();
    }

    public List<Map<String, Object>> queryrandom(Map<String, Object> map) {
        return bm.queryrandom(map);
    }

    public List<Map<String, Object>> queryok(String name, BigDecimal price) {
        return bm.queryok(name, price);
    }


    public Book queryById(int id) {
        return bm.queryById(id);
    }
}




*********************************************************************************
package com.zit.dao;

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 java.io.IOException;
import java.io.InputStream;

public class MybatisHelper {
    protected SqlSessionFactory sf;
    protected SqlSession session;

    public MybatisHelper() {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("conf.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        sf = new SqlSessionFactoryBuilder().build(is);
        session = sf.openSession();
    }

    public SqlSession getSqlSession() {

        return session;
    }

    public SqlSessionFactory getFactory() {
        return sf;
    }


}

com.zit.entity包下的java  Book.java Classes.java MyStu.java Student.java Teacher.java

package com.zit.entity;

import lombok.Data;

@Data
public class Teacher {
    private int tid;
    private String tname;
    private Classes classes;
}
*********************************************************************
package com.zit.entity;

import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

@Data
public class Book {
    private int id;
    private String name;
    private BigDecimal price;
    private Date btime;
}

***********************************************************************
package com.zit.entity;

import lombok.Data;

@Data
public class Classes {
    private int cid;
    private String cname;
    private Teacher teacher;
}
*********************************************************************
package com.zit.entity;

import lombok.Data;

@Data
public class MyStu {
    private int id;
    private String sname;
    private int sscore;
    private String sdept;
}
********************************************************************
package com.zit.entity;

import lombok.Data;

@Data
public class Student {
    private int id;
    private String name;
    private String gender;
    private int score;
    private String dept;
}

com.zit.mapper包下的内容:

接口:BookMapper.java   MyStuMapper.java StudentMapper.java 

映射文件BookMapper.xml   MyStuMapper.xml  StudentMapper.xml 

package com.zit.mapper;

import com.zit.entity.Book;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public interface BookMapper {

    @Select("select * from book")
    public List<Book> query();

    /*  @Select("select * from book where id=#{id}")*/
    public Book queryById(int id);

    @Insert("insert into book values(null,#{name},#{price},#{btime})")
    public int save(Book book);

    public int deleteById(int id);

    public int updateById(Book book);

    @Select("select count(*) from book")
    public int count();


    @Select("select name from book")
    public List<String> queryNames();

    @Select("select name,price from book")
    public List<Map<String, Object>> queryNameAndPrice();

    @Select("select name,price from book where name like #{name} and price>#{price}")
    public List<Map<String, Object>> queryrandom(Map<String, Object> map);

    //增加注解
    @Select("select name,price from book where name like #{nn} and price>#{pp}")
    public List<Map<String, Object>> queryok(@Param("nn") String name, @Param("pp") BigDecimal price);


}




BookMapper.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="com.zit.mapper.BookMapper">
    <delete id="deleteById">
        delete from book where id=#{id}
    </delete>

    <select id="queryById" resultType="book" parameterType="int">
        select * from book where id=#{id}
    </select>

    <update id="updateById">
        update book set name=#{name},price=#{price},btime=#{btime}  where  id=#{id}
    </update>
</mapper>

*********************************************************************************
package com.zit.mapper;

import com.zit.entity.MyStu;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.StatementType;
import org.apache.ibatis.session.RowBounds;

import java.util.List;

public interface MyStuMapper {
    @Select("select id,name,score,dept from stuu")
    @ResultMap("ms")
    public List<MyStu> query();


    @Select("select count(*) from stu")
    public int getCount();


    @Select("select id,name,score,dept from stu limit #{start},#{pagesize}")
    @ResultMap("ms")
    public List<MyStu> page(@Param("start") int start, @Param("pagesize") int pagesize);


    @Select("select * from stu")
    @ResultMap("ms")
    public List<MyStu> pages(RowBounds rows);


    @Select("call stushow")
    @ResultMap("ms")
    @Options(statementType = StatementType.CALLABLE)
    public List<MyStu> pp();

    public List<MyStu> pppp();


}


MyStuMapper.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="com.zit.mapper.MyStuMapper">
    <resultMap id="ms" type="MyStu">
        <id column="id" property="id"/>
        <id column="name" property="sname"/>
        <id column="score" property="sscore"/>
        <id column="dept" property="sdept"/>
    </resultMap>

    <select id="abc" resultMap="ms" statementType="PREPARED">
        select id,name,score,dept from stuu
    </select>

    <!--string List<String>  map List<Map<String,Object>>-->
    <select id="showdbs" resultType="java.lang.String">
        /*show databases*/
        <include refid="dbs"/>
    </select>


    <sql id="tbs">show tables</sql>

    <sql id="dbs">show databases</sql>

    <select id="pppp" resultMap="ms" statementType="CALLABLE">
    call stushow
    </select>


</mapper>



**********************************************************************************

package com.zit.mapper;

import com.zit.entity.Student;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface StudentMapper {


    //public List<Student> query();

    //public List<Student> query(@Param("name") String name);

    public List<Student> query(Map<String, Object> map);

    // public int update(Student student);
    public int update(Map<String, Object> map);

    @Select("select * from student where id=#{id}")
    public Student queryById(int id);


}
StudentMapper.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="com.zit.mapper.StudentMapper">

    <select id="query" resultType="student" parameterType="map">
        select id,name,gender,score,dept from student
        <where>
            <if test="name!=null">
                <if test='name.contains("%")'>
                    and name like #{name}
                </if>
                <if test='!name.contains("%")'>
                    and name = #{name}
                </if>
            </if>
            <if test="id!=null">
                and id=#{id}
            </if>
        </where>
    </select>
    <!--   set name=#{name},gender=#{gender},dept=#{dept},score=#{score} where id=#{id}-->
    <update id="update" parameterType="student">
        update student
        <!--  <set>-->
        <trim prefix="SET" suffixOverrides=",">
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="dept!=null">
                dept=#{dept},
            </if>
            <if test="score!=null">
                score=#{score},
            </if>
            <if test="gender!=null">
                gender=#{gender},
            </if>
        </trim>
        <!--   </set>-->
        where id=#{id}
    </update>


</mapper>

*******************************************************************************


com.zit.servlet包:

show.java Update.java

 

package com.zit.servlet;

import com.zit.dao.BookDAO;
import com.zit.entity.Book;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/show")
@Slf4j
public class Show extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        if (req.getParameter("name") != null) {
            BookDAO bd = new BookDAO();
            Book b = new Book();
            b.setName(req.getParameter("name"));
            b.setPrice(new BigDecimal(req.getParameter("price")));
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                d = sdf.parse(req.getParameter("btime"));

            } catch (ParseException e) {
                e.printStackTrace();
            }
            b.setBtime(d);
            bd.save(b);
            resp.sendRedirect("/show");

        } else if (req.getParameter("did") != null) {
            BookDAO bd = new BookDAO();
            bd.deleteById(Integer.parseInt(req.getParameter("did")));
            resp.sendRedirect("/show");

        } else if (req.getParameter("uid") != null) {
            BookDAO bd = new BookDAO();
            req.setAttribute("book", bd.queryById(Integer.parseInt(req.getParameter("uid"))));
            req.getRequestDispatcher("/update.jsp").forward(req, resp);

        }/* else if (req.getParameter("upid") != null) {
            BookDAO bd = new BookDAO();
            Book book = new Book();
            book.setId(Integer.parseInt(req.getParameter("upid")));
            System.out.println(book.getId());
            book.setName(req.getParameter("name"));
            book.setPrice(new BigDecimal(req.getParameter("price")));
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                d = sdf.parse(req.getParameter("btime"));

            } catch (ParseException e) {
                e.printStackTrace();
            }
            book.setBtime(d);
            System.out.println(book);
            //bd.updateById(book);
            resp.sendRedirect("/show");
        } */ else {
            log.debug("book");
            BookDAO bd = new BookDAO();
            req.setAttribute("book", bd.query());
            req.getRequestDispatcher("show.jsp").forward(req, resp);
        }

    }

}

*******************************************************************

package com.zit.servlet;

import com.zit.dao.BookDAO;
import com.zit.entity.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/update")
public class Update extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        BookDAO bd = new BookDAO();
        Book book = new Book();
        book.setId(Integer.parseInt(req.getParameter("upid")));
        book.setName(req.getParameter("name"));
        book.setPrice(new BigDecimal(req.getParameter("price")));
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            d = sdf.parse(req.getParameter("btime"));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        book.setBtime(d);
        bd.updateById(book);
        resp.sendRedirect("/show");


    }

}

src/main/java包下的东西完成。。

src/main/webapp包下的内容:

webapp/WEB-INF/web.xml文件上面已经写过了

web/app其他内容:index.jsp   show.jsp  update.jsp

index.jsp

<%@ page import="com.zit.dao.BookDAO" %>
<%@ page import="com.zit.entity.Book" %>
<%@ page import="java.util.List" %>
<%@ page language="java" pageEncoding="utf-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <mata charset="utf-8"/>
    <title>显示书籍信息</title>
    <meta http-equiv="refresh" content="0;url=/show">
</head>
<body>


</body>
</html>

**************************************************************************

show.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page language="java" pageEncoding="utf-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <mata charset="utf-8"/>
    <title>显示书籍信息</title>
</head>
<body>
<c:forEach items="${book}" var="bk">
    id:${bk.id}----------${bk.name}-----价格:${bk.price}--------出版日期:<fmt:formatDate value="${bk.btime}"
                                                                                   pattern="yyyy年MM月dd日"/>
    ----<a href="/show?did=${bk.id}">删除</a>
    &nbsp;&nbsp;
    <a href="/show?uid=${bk.id}">编辑</a>
    <br>

</c:forEach>
<br>
<form action="/show" method="post">
    书籍名称:<input type="text" name="name"><br>
    书籍价格:<input type="text" name="price"><br>
    出版日期:<input type="text" name="btime"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

*******************************************************************************

update.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page language="java" pageEncoding="utf-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <mata charset="utf-8"/>
    <title>修改书籍信息</title>
</head>
<body>
<h3>修改书籍信息</h3>
<form action="/update?upid=${book.id}" method="post">
    书籍名称:<input type="text" name="name" value="${book.name}"><br>
    书籍价格:<input type="text" name="price" value="${book.price}"><br>
    出版日期:<input type="text" name="btime" value="<fmt:formatDate value="${book.btime}" pattern="yyyy-MM-dd"/> "><br>
    <input type="submit" value="提交更改">
</form>

<a href="/show">查看书籍信息</a>
</body>
</html>

完成。。。

 

src/test/java/com/Demo.java测试文件如下:

package com;


import com.zit.dao.BookDAO;
import com.zit.dao.MybatisHelper;
import com.zit.entity.MyStu;
import com.zit.entity.Student;
import com.zit.mapper.BookMapper;
import com.zit.mapper.MyStuMapper;
import com.zit.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo {
    @Test
    public void msql() {
        SqlSession ss = new MybatisHelper().getSqlSession();
        StudentMapper sdao = ss.getMapper(StudentMapper.class);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 2);
        map.put("name", "周强");
        map.put("dept", "计算机网络");
        sdao.update(map);
        ss.commit();

      /*  Student stu=sdao.queryById(2);
        stu.setName("张三丰");
        stu.setScore(80);
        stu.setDept("武术");
        sdao.update(stu);*/
        /*  ss.commit();*/


        //System.out.println(sdao.query(null));
     /*  Map<String,Object> map=new HashMap<String, Object>();
       map.put("name","李七七");
       map.clear();*/
        // System.out.println(sdao.query(map));
   /*     System.out.println(sdao.query(map));
       // System.out.println(sdao.query(4));
        map.clear();


        System.out.println(sdao.query(map));


        map.put("id",5);
        System.out.println(sdao.query(map));
        map.put("name","___");*/


    }


    @Test
    public void sql() {
        SqlSession ss = new MybatisHelper().getSqlSession();
        MyStuMapper mdao = ss.getMapper(MyStuMapper.class);

        //System.out.println(mdao.pp());
        System.out.println(mdao.pppp());


        //System.out.println(ss.selectList("showdbs"));


    /*    SqlSession aa = new MybatisHelper().getSqlSession();
        System.out.println(ss==aa);
        Connection c1=ss.getConnection();
        Connection c2=aa.getConnection();
        System.out.println(c1==c2);*/


       /* MyStuMapper sdao = s.getMapper(MyStuMapper.class);
        System.out.println(s.selectList("showdbs"));*/
        // Connection conn=s.getConnection();

    }


    @Test
    public void ppp() {
        SqlSession s = new MybatisHelper().getSqlSession();
        MyStuMapper sdao = s.getMapper(MyStuMapper.class);
        RowBounds rows = new RowBounds(3, 3);
        List<MyStu> list = sdao.pages(rows);
        System.out.println(list.size());
        System.out.println(list);
    }


    @Test
    public void abcdd() {
        SqlSession s = new MybatisHelper().getSqlSession();
        MyStuMapper sdao = s.getMapper(MyStuMapper.class);
        int pagesize = 3;
        int currpage = 2;
        int recordcount = sdao.getCount();
        System.out.println(recordcount);
        List<MyStu> li = sdao.page(currpage * pagesize - pagesize, pagesize);
        System.out.println(li.size());
        System.out.println(li);


        /*System.out.println(sdao.query());
        System.out.println(sdao.query().size());*/
        //System.out.println(s.selectList("abc"));
        //System.out.println(s.selectList("com.zit.mapper.MyStuMapper.abc"));

    }


    @Test
    public void nnn() {
        BookDAO bdao = new BookDAO();
        System.out.println(bdao.queryById(10));
    }


    @Test
    public void del() {
        BookDAO bdao = new BookDAO();
        //System.out.println(bdao.count());
        //System.out.println(bdao.queryNames());
        //List<Map<String,Object>>list=bdao.queryNameAndPrice();
  /*      Map<String,Object>map=new HashMap<String, Object>();
        map.put("name","%java%");
        map.put("price",new BigDecimal(150));*/
        //List<Map<String,Object>>list=bdao.queryrandom(map);
        List<Map<String, Object>> list = bdao.queryok("%java%", new BigDecimal(50));
        for (Map m : list) {
            System.out.println(m.get("name") + "-------" + m.get("price"));
        }

    }


    @Test
    public void abc() {
        try {
            Reader reader = Resources.getResourceAsReader("conf.xml");
            SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(reader);
            SqlSession session = sf.openSession();
            BookMapper bookdao = session.getMapper(BookMapper.class);
            //System.out.println(bookdao.query());
            System.out.println(bookdao.queryById(19));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

全部完成。。。。。。。。。。。。。。。。。。。。。。。。。

 

10-04 14:37