Mybatis

Mybatis

1.po

publicclassUser {


privateintid;

privateString username;

privateString sex;

privateDate birthday;

privateString address;

publicintgetId() {

returnid;

}

publicvoidsetId(intid) {

this.id= id;

}

2.povo

//beanVo 类相对于数据库中的视图,封装视图对象


publicclassUserVo {


privateUser user;

privateList<Integer> idList;


publicUser getUser() {

returnuser;

}


publicvoidsetUser(User user){

this.user= user;

}


publicList<Integer> getIdList() {

returnidList;

}


publicvoidsetIdList(List<Integer> idList){

this.idList= idList;

}

}


3.mappermapper类和mapperxml文件放在统一的包下

publicinterfaceUserMapper {

//1、根据用户ID查询用户信息

publicUser findUserById(intid)throwsException;


//3、添加用户

publicvoidinsertUser(User user)throwsException;

//综合查询

publicList<User> findUserList(UserVo vo);

//综合查询用户总数

publicintfindUserCount(UserVo vo);

//resultMap入门

publicUser findUserRstMap(intid);

}


<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEmapper

PUBLIC"-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mappernamespace="com.formssi.mapper.UserMapper">

<!--根据用户ID查询用户信息-->

<selectid="findUserById"parameterType="int"resultType="User">

SELECT

*FROM USER WHERE id =#{id}

</select>

<!--定义sql片段-->

<!--sql片段内,可以定义sql语句中任何部分-->

<!--sql片段内,最好不用将whereselect关键字声明在内-->

<sqlid="whereClause">

<!--if标签:可以对输入的参数进行判断-->

<!--test:指定判断表达式-->

<iftest="user!= null">

<iftest="user.username!= null and user.username != ''">

ANDusername LIKE '%${user.username}%'

</if>

<iftest="user.sex!= null and user.sex != ''">

ANDsex = #{user.sex}

</if>

</if>


<iftest="idList!= null">

<!--AND id IN (#{id},#{id},#{id}) -->


<!--collection:表示pojo中集合属性的属性名称-->

<!--item:为遍历出的结果声明一个变量名称-->

<!--open:遍历开始时,需要拼接的字符串-->

<!--close:遍历结束时,需要拼接的字符串-->

<!--separator:遍历中间需要拼接的连接符-->

ANDid IN

<foreachcollection="idList"item="id"open="("close=")"

separator=",">

#{id}

</foreach>

</if>

</sql>

<!--综合查询,查询用户列表-->

<selectid="findUserList"parameterType="UserVo"

resultType="user">

SELECT* FROM user

<!--where标签:默认去掉后面第一个AND,如果没有参数,则把自己干掉-->

<where>

<!--引入sql片段-->

<includerefid="whereClause"/>

</where>

</select>

<!--综合查询用户总数-->

<selectid="findUserCount"parameterType="com.formssi.po.UserVo"

resultType="int">

SELECTcount(*) FROM user

<!--where标签:默认去掉后面第一个AND,如果没有参数,则把自己干掉-->

<where>

<!--引入sql片段-->

<includerefid="whereClause"/>

</where>

</select>


<!--resultMap入门-->

<!--id标签:专门为查询结果中唯一列映射-->

<!--result标签:映射查询结果中的普通列-->

<resultMaptype="user"id="UserRstMap">

<idcolumn="id_"property="id"/>

<resultcolumn="username_"property="username"/>

<resultcolumn="sex_"property="sex"/>

</resultMap>


<selectid="findUserRstMap"parameterType="int"resultMap="UserRstMap">

Selectid id_,username username_,sex sex_ from user where id =#{id}

</select>


</mapper>

4.连接数据库配置文件

db.driver=com.mysql.jdbc.Driver

db.url=jdbc:mysql://localhost:3306/pdmybatis?useUnicode=true&characterEncoding=utf8

db.username=root

db.password=wanwan

5.全局配置文件SqlMapConfig.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEconfiguration

PUBLIC"-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>


<!--加载java的配置文件或者声明属性信息-->

<propertiesresource="config/db.properties">

<!--<property name="db.username" value="123" />-->

</properties>


<!--自定义别名-->

<typeAliases>

<!--单个别名定义-->

<!--<typeAlias type="com.itheima.mybatis.po.User"alias="user"/> -->


<!--批量别名定义(推荐)-->

<!--package:指定包名称来为该包下的po类声明别名,默认的别名就是类名(首字母大小写都可)-->

<packagename="com.formssi.po"/>

</typeAliases>



<!--配置mybatis的环境信息,与spring整合,该信息由spring来管理-->

<environmentsdefault="development">

<environmentid="development">

<!--配置JDBC事务控制,由mybatis进行管理-->

<transactionManagertype="JDBC"></transactionManager>

<!--配置数据源,采用mybatis连接池-->

<dataSourcetype="POOLED">

<propertyname="driver"value="${db.driver}"/>

<propertyname="url"value="${db.url}"/>

<propertyname="username"value="${db.username}"/>

<propertyname="password"value="${db.password}"/>

</dataSource>

</environment>

</environments>


<!--加载映射文件-->

<mappers>


<!--批量加载映射文件-->

<packagename="com.formssi.mapper"/>

</mappers>

</configuration>


6.测试


publicclassTest {


privateSqlSessionFactory sqlSessionFactory;

@Before

publicvoidsetUp() throwsException {

//读取配置文件

//全局配置文件的路径

Stringresource= "config/SqlMapConfig.xml";

InputStreaminputStream= Resources.getResourceAsStream(resource);


//创建SqlSessionFactory

sqlSessionFactory= newSqlSessionFactoryBuilder().build(inputStream);

}

@After

publicvoidtearDown() throwsException {

}


@org.junit.Test

publicvoidtest() throwsException {


//1.加载全局配置

//2.创建sqlSessionFactory

//3.打开SQLsession

//创建UserMapper对象

SqlSessionsqlSession= sqlSessionFactory.openSession();


//mybatis通过sqlsession来创建代理对象

UserMappermapper =sqlSession.getMapper(UserMapper.class);


Useruser =mapper.findUserById(1);


System.out.println(user);


sqlSession.close();

}


@org.junit.Test

publicvoidtestFindUserList() throwsException {

//创建UserMapper对象

SqlSessionsqlSession= sqlSessionFactory.openSession();

//mybatis通过sqlsession来创建代理对象

UserMappermapper =sqlSession.getMapper(UserMapper.class);


UserVovo = newUserVo();

//User user= new User();

//user.setUsername("东哥");

//user.setSex("1");

//vo.setUser(user);

List<Integer>idList =newArrayList<>();

idList.add(1);

idList.add(2);

idList.add(10);

vo.setIdList(idList);


List<User>list =mapper.findUserList(vo);

intcount =mapper.findUserCount(vo);


System.out.println(list);

System.out.println(count);

sqlSession.close();

}

@org.junit.Test

publicvoidtestFindUserRstMap() throwsException {

//创建UserMapper对象

SqlSessionsqlSession= sqlSessionFactory.openSession();

//mybatis通过sqlsession来创建代理对象

UserMappermapper =sqlSession.getMapper(UserMapper.class);


Useruser =mapper.findUserRstMap(1);

System.out.println(user);

sqlSession.close();

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值