秒杀系统(二)——基础知识回顾MyBatis之mapper代理方式写mybatis的应用程序

目录

1、环境搭建

2、添加实体

3、添加映射文件Person.xml

4、添加接口

5、添加SqlMappingConfig.xml文件

6、编写测试类进行测试

源代码:


 

1、环境搭建

 

2、添加实体

public class Person {

     private int id;

     private String name; // 姓名

     private String sex; // 性别

     private Date birthday; // 生日

     private String address; // 地址

     public int getId() {

         return id;

     }

     public void setId(int id) {

         this.id = id;

     }

     public String getName() {

         return name;

     }

     public void setName(String name) {

         this.name = name;

     }

     public String getSex() {

         return sex;

     }

     public void setSex(String sex) {

         this.sex = sex;

     }

     public Date getBirthday() {

         return birthday;

     }

     public void setBirthday(Date birthday) {

         this.birthday = birthday;

     }

     public String getAddress() {

         return address;

     }

     public void setAddress(String address) {

         this.address = address;

     }

     @Override

     public String toString() {

         return "Person [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address="

                   + address + "]";

     }

   

}

 

3、添加映射文件Person.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.albertyy.mapper.PersonMapper">

     <sql id="consutomsql" >

         <if test="customPerson != null">

              <if test="customPerson.name!=null and customPerson.name!=''">

                       and name = "${customPerson.name}"

                   </if>

                   <if test="customPerson.birthday!=null and customPerson.birthday!='' ">

                       and birthday = "${customPerson.birthday}"

              </if>

         </if>

     </sql>

     <select id="querypersonbyvo" parameterType="personVo" resultType="person">

     select * from Person

         <where>

              <include refid="consutomsql"></include>

         </where>

     </select>

     <select id="querypersonbyid" parameterType="int" resultType="person">

         select id,name,sex,birthday,address from Person where id = #{value}

     </select>

    

     <select id="querypersonbyname" parameterType="java.lang.String" resultType="person">

         select id,name,sex,birthday,address from Person where name like "%${value}%"

     </select>

    

     <insert id="inserperson" parameterType="person">

         <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">

              SELECT LAST_INSERT_ID()

         </selectKey>

         insert into Person(name,sex,address,birthday) value(#{name},#{sex},#{address},#{birthday})

     </insert>

    

     <delete id="deletepersonbyid" parameterType="int">

         delete from Person where id = #{id}

     </delete>

    

     <delete id="updatepersonbyid" parameterType="person">

         update Person set name=#{name},sex=#{sex},birthday=#{birthday},address=#{address} where id = #{id}

     </delete>

    

</mapper>

4、添加接口

public interface PersonMapper {

     public Person querypersonbyid(int id);

     public List<Person> querypersonbyname(String name);

     public void inserperson(Person person);

     public void deletepersonbyid(int id);

     public void updatepersonbyid(Person person);

     public List<Person> querypersonbyvo(PersonVo personVo);

}

5、添加SqlMappingConfig.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>



     <!-- 加载属性文件 -->

     <properties resource="jdbc.properties">

         <!--properties中还可以配置一些属性名和属性值 -->

         <!-- <property name="jdbc.driver" value=""/> -->

     </properties>



     <typeAliases>

         <!-- 别名定义 -->

         <typeAlias type="com.albertyy.entity.Person" alias="Person" />

         <typeAlias type="com.albertyy.vo.PersonVo" alias="PersonVo" />

     </typeAliases>



     <environments default="development">

         <environment id="development">

              <!-- 使用jdbc事务管理,事务控制由mybatis -->

              <transactionManager type="JDBC" />

              <!-- 数据库连接池,由mybatis管理 -->

              <dataSource type="POOLED">

                   <property name="driver" value="${jdbc.driver}" />

                   <property name="url" value="${jdbc.url}" />

                   <property name="username" value="${jdbc.username}" />

                   <property name="password" value="${jdbc.password}" />

              </dataSource>

         </environment>

     </environments>





     <!-- 加载 映射文件 -->

     <mappers>





         <!--通过resource方法一次加载一个映射文件 -->

         <!--注意这里的路径和xml文件 -->

         <mapper resource="Person.xml" />



     </mappers>



</configuration>



6、编写测试类进行测试

public class TestMybatis {

     // 获得工厂

     public SqlSessionFactory getFactory() {

         String filepath = "SqlMappingConfig.xml";

         SqlSessionFactory sqlSessionFactory = null;

         try {

              InputStream in = Resources.getResourceAsStream(filepath);

              sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);

         } catch (IOException e) {

              e.printStackTrace();

         }

         return sqlSessionFactory;

     }



     // 插入

     @Test

     public void testinsert() {

         // 创建Person实体

         Person person = new Person();

         person.setName("藏小组");

         person.setSex("男");

         String date = "2019-01-12";

         // 创建sdf对象,指定日期格式类型

         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

         // sdf将字符串转化成java.util.Date

         java.util.Date parse = null;

         try {

              parse = sdf.parse(date);

         } catch (ParseException e) {

              e.printStackTrace();

         }

         // java.util.Date转换成long

         long time = parse.getTime();

         // 将long转换为java.sql.Date

         Date date2 = new Date(time);



         person.setBirthday(date2);

         person.setAddress("河南郑州");



         // 得到工厂

         SqlSessionFactory sqlSessionFactory = this.getFactory();

         SqlSession sqlSession = sqlSessionFactory.openSession();

         // 插入数据

         PersonMapper personMapper=sqlSession.getMapper(PersonMapper.class);

         personMapper.inserperson(person);

         System.out.println(person.getId());

         sqlSession.commit();

         sqlSession.close();

     }



     // 通过Id查询

     @Test

     public void getByIdPerson() {

         // 得到工厂

         SqlSessionFactory sqlSessionFactory = this.getFactory();

         SqlSession sqlSession = sqlSessionFactory.openSession();



         Person person = sqlSession.getMapper(PersonMapper.class).querypersonbyid(3);

         System.out.println(person);

         sqlSession.commit();

         sqlSession.close();

     }



     // 通过名字模糊查询

     @Test

     public void getByNameList() {

         // 得到工厂

         SqlSessionFactory sqlSessionFactory = this.getFactory();

         SqlSession sqlSession = sqlSessionFactory.openSession();



         List<Person> persons = sqlSession.getMapper(PersonMapper.class).querypersonbyname("小");

         for (Person person2 : persons) {



              System.out.println(person2);

         }

         sqlSession.commit();

         sqlSession.close();

     }



     // 通过Id进行删除

     @Test

     public void deleteById() {

         // 得到工厂

         SqlSessionFactory sqlSessionFactory = this.getFactory();

         SqlSession sqlSession = sqlSessionFactory.openSession();



         sqlSession.getMapper(PersonMapper.class).deletepersonbyid(6);



         sqlSession.commit();

         sqlSession.close();

     }



     // 更新

     @Test

     public void testupdatePerson() throws IOException {

         SqlSessionFactory sqlSessionFactory = this.getFactory();

         SqlSession sqlsession = sqlSessionFactory.openSession();

         Person person = new Person();

         person.setId(2);

         person.setName("AlbertYang666");

         person.setAddress("河南郑州");

         person.setSex("男");



         String date = "2019-01-30";

         // 创建sdf对象,指定日期格式类型

         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

         // sdf将字符串转化成java.util.Date

         java.util.Date parse = null;

         try {

              parse = sdf.parse(date);

         } catch (ParseException e) {

              e.printStackTrace();

         }

         // java.util.Date转换成long

         long time = parse.getTime();

         // 将long转换为java.sql.Date

         Date date2 = new Date(time);

         person.setBirthday(date2);

         sqlsession.getMapper(PersonMapper.class).updatepersonbyid(person);

         sqlsession.commit();

         sqlsession.close();

     }

}

 

源代码:

链接: https://pan.baidu.com/s/1n4Swp3uvkMGTUcx-i-6sNQ 提取码: axux

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__0809 返回首页