mybatis学习使用2简单增删改查
程序员文章站
2022-03-26 09:33:37
mybatis的增删改查代码如下demo: package com.rookie.bigdata.domain; import lombok.Data; import java.io.Serializable; /** * @author * @date 2018/10/21 */ //Data注解 ......
mybatis的增删改查代码如下demo:
user.java
package com.rookie.bigdata.domain; import lombok.data; import java.io.serializable; /** * @author * @date 2018/10/21 */ //data注解可以省略get和set方法 @data public class user implements serializable{ // 用户标识 private integer id; // 用户姓名 private string name; // 用户性别 private string sex; // 用户年龄 private integer age; // 无参数构造器 public user() { super(); } // 有参数构造器 public user( string name, string sex, integer age) { super(); this.name = name; this.sex = sex; this.age = age; } }
usermapper.java
package com.rookie.bigdata.mapper; import com.rookie.bigdata.domain.user; /** * @author * @date 2018/10/21 */ public interface usermapper { void save(user user); void saveuser(user user); user selectuser(integer id); void updateuser(user user); void deleteuser(integer id); }
usermapper.xml
<?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.rookie.bigdata.mapper.usermapper"> <insert id="save" parametertype="com.rookie.bigdata.domain.user" usegeneratedkeys="true"> insert into bs_user(name,sex,age) values(#{name},#{sex},#{age}) </insert> <!-- parametertype="user"表示该插入语句需要一个user对象作为参数 usegeneratedkeys="true"表示使用自动增长的主键,即使用jdbc的getgeeratedkeys方法获取由数据库生疮内部生成的主键,默认为false keyproperty唯一标记的一个属性,mybatis会通过getgerneratedkeys的返回值设置它的键值--> <insert id="saveuser" parametertype="com.rookie.bigdata.domain.user" usegeneratedkeys="true" keyproperty="id"> insert into bs_user(name,sex,age) values(#{name},#{sex},#{age}) </insert> <!-- select操作 parametertype="int"表示该查询语句需要一个int类型的参数 resulttype="com.rookie.bigdata.domain.user"表示返回的是一个user对象 --> <select id="selectuser" parametertype="int" resulttype="com.rookie.bigdata.domain.user"> select * from bs_user where id = #{id} </select> <!-- update操作 parametertype="com.rookie.bigdata.domain.user"表示该更新语句需要一个user对象作为参数--> <update id="updateuser" parametertype="com.rookie.bigdata.domain.user"> update bs_user set name = #{name},sex = #{sex},age = #{age} where id = #{id} </update> <!-- delete操作 parametertype="int"表示该查询语句需要一个int类型的参数--> <delete id="deleteuser" parametertype="int"> delete from bs_user where id = #{id} </delete> </mapper>
mybatis-config.xml
<?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"> <!-- xml 配置文件包含对 mybatis 系统的核心设置 --> <configuration> <!-- <typealiases> <typealias alias="user" type="com.rookie.bigdata.domain.user"/> </typealiases>--> <environments default="mysql"> <!-- 环境配置,即连接的数据库。 --> <environment id="mysql"> <!-- 指定事务管理类型,type="jdbc"指直接简单使用了jdbc的提交和回滚设置 --> <transactionmanager type="jdbc"/> <!-- datasource指数据源配置,pooled是jdbc连接对象的数据源连接池的实现。 --> <datasource type="pooled"> <property name="driver" value="com.mysql.jdbc.driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </datasource> </environment> </environments> <!-- mappers告诉了mybatis去哪里找持久化类的映射文件 --> <mappers> <mapper resource="mapper/usermapper.xml"/> </mappers> </configuration>
测试代码usermappertest.java
package com.rookie.bigdata.mapper; import com.rookie.bigdata.domain.user; import org.apache.ibatis.annotations.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 org.junit.before; import org.junit.test; import java.io.ioexception; import java.io.inputstream; /** * @author * @date 2018/10/21 */ public class usermappertest { public sqlsession session; @before public void before() throws ioexception { // 读取mybatis-config.xml文件 inputstream inputstream = resources.getresourceasstream("mybatis-config.xml"); // 初始化mybatis,创建sqlsessionfactory类的实例 sqlsessionfactory sqlsessionfactory = new sqlsessionfactorybuilder() .build(inputstream); // 创建session实例 session = sqlsessionfactory.opensession(); } @test public void saveuser() throws exception { usermapper mapper = session.getmapper(usermapper.class); mapper.saveuser(new user("zhangsan","男",23)); session.commit(); // 关闭session session.close(); } @test public void selectuser() throws exception { usermapper mapper = session.getmapper(usermapper.class); user user = mapper.selectuser(5); // 提交事务 session.commit(); // 关闭session session.close(); system.out.println(user); } @test public void updateuser() throws exception { usermapper mapper = session.getmapper(usermapper.class); user user = mapper.selectuser(5); user.setage(21); mapper.updateuser(user); // 提交事务 session.commit(); // 关闭session session.close(); system.out.println(user); } @test public void deleteuser() throws exception { usermapper mapper = session.getmapper(usermapper.class); mapper.deleteuser(5); session.commit(); // 关闭session session.close(); } }
关联查询的代码如下:
staff.java
package com.rookie.bigdata.domain; import lombok.data; /** * @author * @date 2018/10/21 */ @data public class staff { private integer id; private string name; private string sex; private integer age; private department department; }
department.java
package com.rookie.bigdata.domain; import lombok.data; import java.util.list; /** * @author liuxili * @date 2018/10/21 */ @data public class department { private integer id; private string code; private list<staff> staffs; }
companymapper.java
package com.rookie.bigdata.mapper; import com.rookie.bigdata.domain.department; import com.rookie.bigdata.domain.staff; import java.util.list; /** * @author * @date 2018/10/21 */ public interface companymapper { list<staff> selectstaff(); list<department> selectdepartment(); }
companymapper.java
<?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.rookie.bigdata.mapper.companymapper"> <!--职工映射对象--> <resultmap id="staffresultmap" type="com.rookie.bigdata.domain.staff"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <!--进行关联映射--> <association property="department" column="depart_id" javatype="com.rookie.bigdata.domain.department" select="selectid"/> </resultmap> <!--根据id查询相关部门--> <select id="selectid" resulttype="com.rookie.bigdata.domain.department"> select * from bs_department where id=#{id}; </select> <select id="selectstaff" resultmap="staffresultmap"> select * from bs_staff; </select> <resultmap id="departresultmap" type="com.rookie.bigdata.domain.department"> <id property="id" column="id"/> <result property="code" column="code"/>
<!--javatype:属性对应的类型
oftype:表示集合当中的类型--> <collection property="staffs" javatype="arraylist" column="id" oftype="com.rookie.bigdata.domain.staff" select="selectstaffbyid"/> </resultmap> <select id="selectstaffbyid" resulttype="com.rookie.bigdata.domain.staff"> select * from bs_staff where depart_id=#{id} </select> <select id="selectdepartment" resultmap="departresultmap"> select * from bs_department; </select> </mapper>
测试代码companymappertest.java
package com.rookie.bigdata.mapper; import com.rookie.bigdata.domain.department; import com.rookie.bigdata.domain.staff; 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 org.junit.before; import org.junit.test; import java.io.ioexception; import java.io.inputstream; import java.util.list; import static org.junit.assert.*; /** * @author * @date 2018/10/21 */ public class companymappertest { public sqlsession session; @before public void before() throws ioexception { // 读取mybatis-config.xml文件 inputstream inputstream = resources.getresourceasstream("mybatis-config.xml"); // 初始化mybatis,创建sqlsessionfactory类的实例 sqlsessionfactory sqlsessionfactory = new sqlsessionfactorybuilder() .build(inputstream); // 创建session实例 session = sqlsessionfactory.opensession(); } @test public void selectstaff() throws exception { companymapper mapper = session.getmapper(companymapper.class); list<staff> staff = mapper.selectstaff(); for (staff staff1 : staff) { system.out.println(staff1); } session.commit(); session.close(); } @test public void selectdepartment(){ companymapper mapper = session.getmapper(companymapper.class); list<department> departments = mapper.selectdepartment(); for (department department : departments) { list<staff> staffs = department.getstaffs(); for (staff staff : staffs) { system.out.println(department); system.out.println(staff); } } } }
上一篇: asp.net如何生成图片验证码(简单)
下一篇: 社交电商进入农村,看看都有啥新玩法?