发表于: 2018-02-26 01:20:36

2 595


今日完成

  1.重写spring+mybatis整合,使用注解方配置mybatis;

   UserMapper2 

public interface UserMapper2 {

@Select("select * from user where id=#{id}")
public User2 findUserById(int ID) throws IOException;//查询
       @Select("select * from user where username like  '%${value}%'")
public List<User2> findUserByname(String name)throws IOException;
       @Delete("delete from user where id=#{id}")
public  void deleteUser(int id) throws  Exception;//删除指定id的记录
       @Update("update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}")
public void updateUser(User2 user2) throws Exception;//更改指定id的数据

}

bean

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
   <context:property-placeholder location="db.properties"/>
   <bean id="dataSource" class="${dataSource}" destroy-method="close">
       <property name="username" value="${jdbc.username}"/>
       <property name="driverClassName" value="${jdbc.driver}"/>
       <property name="url" value="${jdbc.url}"/>
       <property name="password" value="${jdbc.password}"/>
   </bean>
   <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="configLocation" value="SqlmapConfig2.xml"/>
       <property name="dataSource" ref="dataSource"/>
   </bean>
   <bean name="UserMapper2" class="org.mybatis.spring.mapper.MapperFactoryBean">
       <property name="mapperInterface" value="UserMapper2"/>
       <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
   </bean>
</beans>

UserMapper2.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="UserMapper2">
</mapper>

mybatis全局配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
   <mappers>
       <mapper class="UserMapper2"/>
   </mappers>
</configuration>

测试类

public class Spring_MbatisTestAnnotation {
private ApplicationContext applicationContext;
@Before
   public void setup() {
applicationContext= new ClassPathXmlApplicationContext("Springconfig.xml");
   }

@Test
   public void findUserByID ()throws Exception {
UserMapper2 userMapper2=(UserMapper2) applicationContext.getBean("UserMapper2");
       User2 user2= userMapper2.findUserById(5);
       System.out.println(user2);
   }
}

  2.参照阿里巴巴开发手册修改spring+mybatis整合的代码;

   目录结构如下:

    测试类

public class StudentTest {
private ApplicationContext applicationContext;

   @Before
   public void setup() {
applicationContext = new ClassPathXmlApplicationContext("com/student/StudentBeans.xml");
   }

@Test
   public void findUserById() throws IOException {
StudentMapper studentMapper = (StudentMapper) applicationContext.getBean("student");
       Student student = studentMapper.findUserById(2);
       System.out.println(student);
   }

@Test
   public void findUserByName() throws IOException {
StudentMapper studentMapper = (StudentMapper) applicationContext.getBean("student");
       List<Student> student = studentMapper.findUserByName("姚%");
       System.out.println(student);
   }

@Test
   public void deleteUser() throws Exception {
StudentMapper studentMapper = (StudentMapper) applicationContext.getBean("student");
       int a=studentMapper.deleteUser(38);
       System.out.println(a);
   }
@Test
   public void updateUser() throws Exception {
StudentMapper studentMapper = (StudentMapper) applicationContext.getBean("student");
       Student student=new Student(null,"我的",23333,"sapm-10",8678L,
       "snsdao","bdiasundka",
               "sndoasl","22",null,null);
       student.setID(43L);
       int a=studentMapper.updateUser(student);
       System.out.println(a);
   }
@Test
   public void insertUser() throws Exception {
StudentMapper studentMapper = (StudentMapper) applicationContext.getBean("student");
       Student student=new Student(null,"ta的",224,"sapm-11",231412685L,
               "asaf","bdiasundka",
               "saca","22",null,null);
     for(int i=0;i<1000;i++) {
studentMapper.insertUser(student);
         Long a = student.getID();
         System.out.println(a);
     }
  }
}

   3.完善表格,填了以前留下的坑;

   4.查看任务一验收标准,修改一些代码;

明天计划

   1.log4j知识点遗忘,复习并深入学习;

遇到问题

---------

收获

mybatis基于注解的实现増删改查和代码规范;


返回列表 返回列表
评论

    分享到