取别名package
<!-- 通过package, 可以直接指定package的名字, mybatis会自动扫描你指定包下面的javabean, 并且默认设置一个别名,默认的名字为: javabean 的首字母小写的非限定类名来作为它的别名 -->
<typeAliases>
<package name="pd.entity"/>
</typeAliases>
取别名alias
<!-- type指的是javabean的完全限定名 alias就是指代别名-->
<typeAliases>
<typeAlias type="pd.entity.Book" alias="Book" />
</typeAliases>
resultMap
<resultMap type="PanDaEntity" id="entity1">
<id property="uid"/>
<!-- property是实体类名字 column是数据库字段名 -->
<result property="uname" column="unama"/>
<result property="upwd"/>
<result property="uphone"/>
<result property="uaddress"/>
<result property="ubirthday"/>
</resultMap>
查
<!-- 查 -->
<select id="getTableByTid" resultMap="tables">
select *from tablename1 where
<if test="tid!=null">
<!--左边是数据库字段名 右边是实体类名字 -->
tid=#{tid}
</if>
</select>
改
<update id="updateByTname" parameterType="String">
update tablename1
<set>
<if test="tname!=null">
tname=#{tname}
</if>
</set>
<where>
<if test="tid!=null">
tid=#{tid}
</if>
</where>
</update>
删
<delete id="deleteByTid" parameterType="int">
delete from tablename1
<where>
<if test="tid!=null">
tid=#{tid}
</if>
</where>
</delete>
增
<insert id="insertByTid" parameterType="String">
<!-- 插入要在占位符后加入jdbcType=数据库的类型 -->
insert into tablename1 values(#{tid,jdbcType=VARCHAR},#{tname,jdbcType=VARCHAR})
</insert>
@Param注解
//@Param("uid")Integer uid对应的是实体类
List<PanDaEntity> select3(@Param("uid")Integer uid,@Param("uname")String uname);
接口类
package pd.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import pd.entity.Book;
import pd.entity.Group;
import pd.entity.Person;
import pd.entity.Table;
import pd.entity.User;
public interface TestDao {
List<User> getUser1();
List<User> getUser2();
List<Book> getBooks();
List<Person> getPersonList();
List<Group> getGroupList();
List<Book> getBooks2();
List<Table> getTable();
List<Table> getTableByTid(@Param("tid")String tid);
int deleteByTid(@Param("tid")String tid);
int insertByTid(@Param("tid")String tid,@Param("tname")String tname);
int updateByTname(@Param("tname")String tname,@Param("tid")String tid);
}
测试类
package test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import pd.dao.TestDao;
import pd.entity.Book;
import pd.entity.Group;
import pd.entity.Person;
import pd.entity.Table;
import pd.entity.User;
import pd.untils.CopyOfPandaUntils;
public class Test {
public static void main(String[] args) {
SqlSession session=null;
try {
session=CopyOfPandaUntils.getInstance().getSession();
/*单表查询
List<User>list=session.getMapper(TestDao.class).getUser1();
for(User bb:list){
System.out.println(bb);
}*/
/*一对多
List<User>list=session.getMapper(TestDao.class).getUser2();
for(User bb:list){
System.out.println(bb.toString());
}*/
/*一对一
* List<Book>list=session.getMapper(TestDao.class).getBooks();
for(Book bb:list){
System.out.println(bb.toString());
}*/
/*多对多查询
* List<Person>list=session.getMapper(TestDao.class).getPersonList();
for(Person bb:list){
System.out.println(bb.toString());
}*/
/*List<Book>list=session.getMapper(TestDao.class).getBooks2();
for(Book bb:list){
System.out.println(bb.toString());
}*/
/*单表全查
* List<Table>list=session.getMapper(TestDao.class).getTable();
for(Table bb:list){
System.out.println(bb.toString());
}*/
//根据ID查询
/* List<Table>list=session.getMapper(TestDao.class).getTableByTid("1");
for(Table bb:list){
System.out.println(bb.toString());
}*/
/*删除
* int count=session.getMapper(TestDao.class).deleteByTid("5");
System.out.println(count);*/
// 插入
int count=session.getMapper(TestDao.class).insertByTid("7", "liu");
System.out.println(count);
/*更改
* int count=session.getMapper(TestDao.class).updateByTname("cool", "5");
System.out.println(count);*/
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
CopyOfPandaUntils.closeSession(session);
}
}
}