发表于: 2017-06-22 21:47:50
2 1067
今天完成的事情:
完成最基本的mybatis 增删改查,但是报一个Class not found: "com.db1.mapper.TestTable1"Empty test suite.
<?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>
<typeAliases>
<package name="com.db1.mapper"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysq1//127.0.0.1:3306/db1"/>
<property name="username" value="root"/>
<property name="password" value="luozeying"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/db1/mapper/Table1.xml"/>
</mappers>
</configuration>
<?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.db1.mapper">
<select id="getTable1" parameterType="int" resultMap="Table1">
select * from table1 where id = #{id}
</select>
<select id="listTable1" resultType="Table1">
select * from table1
</select>
<insert id="addTable1" parameterType="Table1">
insert into
table1 (id,updata_at,create_at,name,qq,jobs,join_time,school,online_id,data_url,declaration,teacher,sources)
values(#{id},#{updata_at},#{create_at},#{name},#{qq},#{jobs},#{join_time},#{school},#{online_id},#{data_url})
</insert>
<update id="updateTable1" parameterType="Table1">
update table1
set #{id},#{updata_at},#{create_at},#{name},#{qq},#{jobs},#{join_time},#{school},#{online_id},#{data_url},#{declaration},#{teacher},#{sources}
where Table1 = #{Table1 }
</update>
<delete id="deleteTable1" parameterType="Table1" >
delete from table1
where id= #{id}
</delete>
</mapper>
package com.db1.mapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Created by PC on 2017/6/22.
*/
public class TestMybatis {
public static void main(String [] args) throws IOException{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build( inputStream );
SqlSession session = sqlSessionFactory.openSession();
listALL(session);
session.commit();
session.close();
}
private static void update(SqlSession session) {
Table1 t= session.selectOne("getCategory",3);
t.setName("修改了的Category名稱");
session.update("updateCategory",t);
listALL(session);
}
private static void get(SqlSession session){
Table1 t = session.selectOne("getCategory",9);
System.out.println( t.getName() );
}
private static void delete(SqlSession session) {
Table1 t = new Table1();
t.setId( 12 );
session.delete("deleteTable1",t);
listALL(session );
}
private static void add(SqlSession session){
Table1 t = new Table1();
t.setName( "新增加的Table" );
session.insert("addTable1",t);
listALL( session );
}
private static void listALL(SqlSession session) {
List<Table1> cs = session.selectList( "listTable1" );
for (Table1 t : cs)
System.out.println( t.getName() );
}
}
很奇怪,因为这是照的别人的例子,不知道怎么会这样的。百度说找翻译说这个是一个空的测试套件
明天需要完成的事:明天要请假,请假这些天补补基础知识,和去网上找这个一个问题的解决办法
遇到的问题:这个mybatis一直过不去,找不到原因,不管写多少个例子都很难进行下去。
还有这个Class not found: "com.db1.mapper.TestTable1"Empty test suite.没有找到原因,以及不知道为什么别人不要,而我的汇报这个错。
收获: 知道了这一个很简单的配置的mybatis。
评论