Servlet
@WebServlet("/demo2")
public class servlet_demo2 extends HttpServlet {/*** 就绪/服务方法(处理请求数据)* 系统方法,服务器自动调用* 当有请求到达servlet时,就会调用该方法* 方法可以被多次调用* @param req* @param resp* @throws ServletException* @throws IOException*/@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("被调用了");}/*** 销毁方法* 系统方法,服务器自动调用* 当服务器关闭或应用程序停止时,调用该方法* 方法只会执行一次*/@Overridepublic void destroy() {System.out.println("Servlet被销毁了");}/*** 初始化方法,系统方法服务器自动调用* 当请求到达Servlet容器时,Servlet容器会判断Servlet对象是否存在,如果不存在则创建实例并初始化* 方法只会执行一次* @throws ServletException*/@Overridepublic void init() throws ServletException {System.out.println("被创建了");}
}
HttpServletRequest
HttpServltResponse:
MyBatis:
接口:
package servlet.mapper;import org.apache.ibatis.annotations.Param;
import servlet.pojo.testdemo1;import java.util.List;public interface UserMapper {//查找所有List<testdemo1> selectAll(); //唯一id//很多个就用集合,单个的就不用//查找单独testdemo1 selectById(int id);//条件查询/*参数接收1.散装参数2.对象参数3.map集合参数*///散装参数:List<testdemo1> selectmany (@Param("id")int id,@Param("username")String username,@Param("password")String password);//param里的字符串要和#{}里面的一样//动态查找List<testdemo1> selectmany_dynamic (@Param("id")int id,@Param("username")String username,@Param("password")String password);//单条件动态查询List<testdemo1> Single_condition_dynamic (@Param("id")int id,@Param("username")String username,@Param("password")String password);//添加void add(testdemo1 testdemo);//修改 可以返回void也可以返回int,返回int就是返回影响的行数int update(testdemo1 testdemo);void delete(int id);void batch_delete(@Param("ids")int [] ids);
}
实现类:
package servlet.mybatis_test;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 servlet.mapper.UserMapper;
import servlet.pojo.testdemo1;import java.io.IOException;
import java.io.InputStream;
import java.util.List;/*mybatis 增删改查*/
public class testdemo4 {public static void main(String[] args) throws IOException {//1.加载mybatis的核心配置文件,获取SqlSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.获取SqlSession对象,用它来执行sql(可以在这里设置自动提交事务)SqlSession sqlSession = sqlSessionFactory.openSession(true);//3.获取UserMapper接口的代理对象UserMapper usermapper = sqlSession.getMapper(UserMapper.class);//查看所有List<testdemo1> users = usermapper.selectAll();System.out.println("这是查询所有:");System.out.println(users);System.out.println();//查看单独int ids = 1;testdemo1 selectById = usermapper.selectById(ids);System.out.println("这是查询单独:");System.out.println(selectById);System.out.println();//条件查询:散装int id2 = 3;String username = "王";String password = "412";//处理参数(因为username是模糊查询)username = "%" + username + "%";List<testdemo1> selectmany = usermapper.selectmany(id2, username, password);System.out.println("这是多条件和模糊查找");System.out.println(selectmany);System.out.println();//动态查询:int id3 = 3;String username3 = "";String password3 = null;//处理参数(因为username是模糊查询)username3 = "%" + username3 + "%";List<testdemo1> selectmany_dynamic = usermapper.selectmany_dynamic(id3, username3, password3);System.out.println("动态查询");System.out.println(selectmany_dynamic);System.out.println();//单条件动态查询:(只要有一个匹配了东西下面的就不会做了)//这个的用处是一个下拉项可能有几个固定选项,从固定选项里面选int id4 = 1;String username4 = "cascaca";String password4 = "djijdadqajdif";//处理参数(因为username是模糊查询)username4 = "%" + username4 + "%";List<testdemo1> single_condition_dynamic = usermapper.Single_condition_dynamic(id4, username4, password4);System.out.println("单条件动态查询");System.out.println(single_condition_dynamic);System.out.println();String username5 = "赵六";String password5 = "23223232";String gender = "男";String addr = "广东";testdemo1 testdemo = new testdemo1();testdemo.setAddr(addr);testdemo.setGender(gender);testdemo.setUsername(username5);testdemo.setPassword(password5);System.out.println("新增数据:");usermapper.add(testdemo);//获取主键必须要给useGeneratedKeys="true" keyProperty="id"Integer id = testdemo.getId();System.out.println(id);/*提交事务:没有的话数据库不会出来新增的数据*//*sqlSession.commit();*/ /*上面开启了自动提交事务所以就不用再写了*//*修改*/int id6=5;String username6 = "七七";String password6 = "52253";String gender6 = "女";String addr6 = "重庆";testdemo1 demo2 = new testdemo1();demo2.setAddr(addr6);demo2.setGender(gender6);demo2.setUsername(username6);demo2.setPassword(password6);demo2.setId(id6);System.out.println("修改成功,受影响的行数:");int count = usermapper.update(demo2);System.out.println(count);/*删除*/int id7=7;System.out.println("删除成功");usermapper.delete(id7);//批量删除/*int[] ids_={6,8};System.out.println("批量删除成功");usermapper.batch_delete(ids_);*///4.释放资源sqlSession.close();}
}
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"><!--namespace:名称空间
-->
<mapper namespace="servlet.mapper.UserMapper"><!-- 这里写增删改查的--><!--id是语句的唯一标识,resultTyle是返回结果的类型--><select id="selectAll" resultType="testdemo1"> /*用了别名所以resultTyle默认就是去servlet.pojo下去看*/select * from test1 ;</select><!--参数占位符1.#{}:会替换成? 2.${}:直接拼sql 会存在sql注入的问题 --><!--注意:sql语句中如果两个比大小要用到小于号(<)时,不能直接写1.转义字符:直接用<代替<号--><select id="selectById" resultType="testdemo1">select * from test1 where id=#{id};</select><select id="selectmany" resultType="testdemo1">select *from test1whereid =#{id}and username like #{username} <!--有like的是模糊查找,比如这里输入华为,那么华为有限公司就可以被找到--><!--注意模糊查找不用=号-->and password =#{password}</select><select id="selectmany_dynamic" resultType="testdemo1">select *from test1where 1 = 1 <!--这里有个恒等式,因为如果id为空的时候 ,where后面就直接接and了,第二种可以直接用where标签括起来,idea会自动识别--><if test="id != null" >and id =#{id}</if><if test="username!= null and username!= '' ">and username like #{username} <!--有like的是模糊查找,比如这里输入华为,那么华为有限公司就可以被找到--><!--注意模糊查找不用=号--></if><if test="password !=null and password !='' ">and password =#{password}</if></select><select id="Single_condition_dynamic" resultType="testdemo1">select *from test1<where><choose><!--相当于switch--><when test="id != null"> <!--相当于case-->id =#{id}</when><when test="username!= null and username!= ''"> <!--相当于case-->username like #{username}</when><when test="password !=null and password !=''"> <!--相当于case-->password =#{password}</when><otherwise> /*这是防止用户一个也不选的情况会报错*/1 = 1 /*如果上面的一个也没选才会做这个*//*但其实我这个用where标签包起来了,其实不写这个也不会报错*/</otherwise></choose></where></select><insert id="add" useGeneratedKeys="true" keyProperty="id">insert into test1(username,password,gender,addr)value (#{username},#{password},#{gender},#{addr})</insert><update id="update">update test1set username = #{username},password = #{password},gender = #{gender},addr = #{addr}where id = #{id};</update><delete id="delete">delete from test1 where id = #{id};</delete><delete id="batch_delete"><!--mybatis会将数组参数,封装成一个Map集合*默认 array=数组*使用@param注解改变map集合的默认key的名称-->delete from test1where id in(<foreach collection="ids" item="id" separator=","> /*因为现在是不确定有几个id要删除所以要这样写*/#{id}</foreach>)</delete></mapper>