发表于: 2017-06-30 19:31:15
2 1043
看的时间不多,主要是写了Junit测试类和看索引去了
spring的Junit测试在测试类前加上
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
如果有多个spring的配置文件Configuration如下
@ContextConfiguration(locations = { "classpath*:spring-ctx-application.xml", "classpath*:spring-ctx-consumer.xml" })
//测试全部通过
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:contextConfig.xml"})
public class Test {
@Autowired
private StudentService studentService;
@org.junit.Test
public void selectAllStudent(){
List<Student> list= studentService.selectAllStudent();
for (Student student:list){
System.out.println(student.toString());
System.out.print("\n");
}
}
@org.junit.Test
public void addStudent(){
Student student = new Student();//student类听说最好不要自己new但是测试类无所谓了。
student.setUsername("1");
student.setQq("1");
student.setOnlineId("1");
student.setExpectStudentTime("1");
student.setType("1");
student.setWish("1");
student.setGraduateSchool("1");
studentService.addStudent(student);
List<Student> list= studentService.selectAllStudent();
for (Student student1:list){
System.out.println(student1.toString());
System.out.print("\n");
}
}
@org.junit.Test
public void selectStudentByname(){
List<Student> student = studentService.selectStudentByname("1");
for (Student student1:student){
System.out.println(student1.toString());
}
}
}
接下来是索引的问题:
在不读取整个表的情况下,索引使数据库应用程序可以更快地查找数据。索引就是用于加快sql搜索的东西,但是pc上面没敢弄太大的数据,看不出来索引的用处。根据以下博客
http://blog.csdn.net/zhongguoren666/article/details/6752153可知,只要不在乎空间,索引可以随便添加。所以目前来说就只了解索引的建立方式,不去深入了解。
基本语句
CREATE INDEX index_name ON table_name (column_name)
完整语句
CREATE [UNIQUE][CLUSTERED | NONCLUSTERED] INDEX index_name
ON {table_name | view_name} [WITH [index_property [,....n]]
然后就是今天遇到的问题,虽然都是比较蠢的问题- -
1:
<!--设置mapper的接口创建MapperBean-->
<bean class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="Mapper.studentMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
第二个property报错,显示SQLSessionFactory的ref必须为org.ibatis.SQLSessionFactoryBean
错误原因是maven没有添加Mybatis的依赖
这里顺便贴一下SSM的依赖,以防以后忘记
springframework的依赖有
spring-context
spring-beans
spring-webmvc
spring-jdbc
spring-aspectj
Mybatis的依赖有
org.Mybatis mybatis
mybatis-spring
mysql的依赖有
mysql-connector-java
log4j的依赖
其他的包需要再去查
2:Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.panda.test1.User with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.panda.test1.User.<init>()
原因如下:
1. model中的get/set方法与成员变量不一。
2. 构造函数被重载过,但是没有空的构造函数。
3. 最好不要使用简单类型,如int, long等,改用对象模式Integer, Long等。在写条件查询时会用到判断<if xxx != null > … </if>的
自己的原因是实体类的构造函数被重载了,手动添加一个无参构造方法就行了。
评论