发表于: 2017-08-21 23:37:49

1 1100


今天完成的事

结束spring+mybatis的代码操作

初步认识JAVA注解

1. @Override——当我们想要复写父类中的方法时,使用该注解去告知编译器我们要复写这个方法。当父类中的方法移除或者发生更改时编译器将报错,配置依赖pom就不说了 参考我上一篇日报

1 写应用类 对应数据库中的表  对应好表中的数据类型

public class Hero {
int id;
       String name;
       int blood;
       int defence;


      public void HeroMod(int id,String name,int blood,int defence) {
this.id = id;
          this.blood = blood;
          this.defence = defence;
          this.name = name;
      }
public void setId(int id){this.id = id;}
public void setName(String name){this.name = name;}
public void setBlood(int blood){this.blood = blood;}
public void setDefence(int defence){this.defence = defence;}
public int getId(){return id;}
public String getName(){return name;}
public int getBlood(){return blood;}
public int getDefence(){return defence;}

@Override
   public String toString(){
return "Hero[id = "+id+",name = "+name+",blood = "+blood+",defence = "+defence+"]"+"\n\n";
   }
}

2 写接口Mapper(增删改查+遍历)方法接口 要通过什么属性执行SQL语句就可以先写入括号中,方法可以分为两类,一种是要深入到该对象的属性中操作,比如UPDATE,另一种是直接操作该对象,不需要知道对象内部的属性是什么样,比如DELETE,,所以我的DELETE就直接通过ID操作对象

public interface HeroMapper {
public int heroInsert(Hero hero);
   public int heroDelete(int id);
   public int heroUpdate(Hero hero);
   public List<Hero> listAll();
   public Hero heroSelect(Hero hero);
}

3.配置mapper.xml   (每个接口要用的SQL语句) 需要注意SQL语句的正确使用

4.ApplicationContext.xml  数据库链接,连接池,SQL注入工厂  需要注意全限定类名是否正确,否则Spring将无法使用该文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
       <property name="driverClassName"
                 value="com.mysql.jdbc.Driver"/>
       <property name="url"
                 value="jdbc:mysql://localhost:3306/yaheng?characterEncoding=utf8&amp;useSSL=false" />
       <property name="username"
                 value="root" />
       <property name="password" value="0123." />
   </bean>
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
       <property name="configLocation" value=""/>
       <property name="mapperLocations" value="classpath:HeroMapper.xml" />
   </bean>
   <bean id="heroMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
       <property name="mapperInterface" value="Mapper.HeroMapper"/>
       <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
   </bean>

5.mybatis-cfg.xml  锁定Mapper位置

6.单元测试直接怼

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:ApplicationContext.xml"})


public class HeroMapperTest {

@Autowired
   ApplicationContext ctx;
   private Hero entityTemp;

   @BeforeClass
   public static void begin(){
System.out.println("测试开始");
   }
@AfterClass
   public static void end(){
System.out.println("测试结束");
   }
@Test
 public void heroUpdate(){
System.out.println("测试修改...");
       HeroMapper heroMapper = (HeroMapper) ctx.getBean("heroMapper");
       Hero hero1 = new Hero();
       hero1.setName("蛮王");
       hero1.setId(13);
       heroMapper.heroUpdate(hero1);
   }
@Test
 public void heroDelete(){
System.out.println("测试删除...");
       HeroMapper heroMapper = (HeroMapper) ctx.getBean("heroMapper");
       heroMapper.heroDelete(14);
 }
@Test
 public void heroInsert(){
System.out.println("测试插入...");
     HeroMapper heroMapper = (HeroMapper) ctx.getBean("heroMapper");
     Hero hero2 = new Hero();
     hero2.setName("光辉");
     hero2.setBlood(700);
     hero2.setDefence(30);
     heroMapper.heroInsert(hero2);
 }
@Test
 public void heroSelect(){
System.out.println("测试查找...");
       HeroMapper heroMapper = (HeroMapper) ctx.getBean("heroMapper");
       Hero hero = new Hero();
       hero.setId(3);
       heroMapper.heroSelect(hero);
       System.out.println(heroMapper.heroSelect(hero));
 }
@Test
   public void listAll(){
System.out.println("测试批量查找...");
     HeroMapper heroMapper = (HeroMapper) ctx.getBean("heroMapper");
     List<Hero> listAll = heroMapper.listAll();
     System.out.println(listAll.toString()+"\n\n");
 }
}

测试

完美!!!

总的来说Spring+mybatis是将Spring的IOC容器和依赖注入的优点,和Mybatis的对SQL语句的封装结合在一起,方便使用和修改,达到了对数据库非常方便的访问和操作


遇到的问题

 <property name="sqlSessionFactory" ref="sqlSessionFactory"/>

   问题因为mybatis的JAR包没有添加成功,添加后解决

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found

问题原因 mapper.xml中的select方法名少一个字母,无法找到方法,加上字母运行成功

明天的计划 

迎接新学院,小课堂彩排,拆分禅道

收获:前期很困难的拿到师兄的代码参考,发现师兄那边有很多报错,先解决了师兄的错误,把代码看懂,回到自己这边虽然出现问题,也能及时解决,看着报错消失,心里很兴奋!

任务进度:任务1,步骤21

任务开始时间2017-8-15

任务结束时间2017-8-25

无延期风险

禅道:禅道挂了。。。。。





返回列表 返回列表
评论

    分享到