当前位置: 首页 > news >正文

建立什么网站赚钱电商软文范例100字

建立什么网站赚钱,电商软文范例100字,wordpress 断点调试,公司部门一般有哪些MyBatis 注解开发详解 MyBatis 支持使用注解来进行数据库操作。注解方式将 SQL 语句直接写在 Java 接口中,通过注解来完成 CRUD(增删改查)操作,省去了使用 XML 配置的繁琐步骤。这种方式适合简单项目或快速原型开发,因…

MyBatis 注解开发详解

MyBatis 支持使用注解来进行数据库操作。注解方式将 SQL 语句直接写在 Java 接口中,通过注解来完成 CRUD(增删改查)操作,省去了使用 XML 配置的繁琐步骤。这种方式适合简单项目或快速原型开发,因为它更直观,可以直接在代码中查看和修改 SQL 语句。

然而,在实际开发中,更推荐使用 XML 配置的方式进行 MyBatis 开发。这样做的主要好处是,当需要修改 SQL 语句时,不需要更改 Java 代码,避免频繁修改源代码带来的风险。此外,XML 配置文件的形式更有利于 SQL 语句的管理和维护,尤其是在项目复杂度较高、SQL 语句较多的情况下。

以下是使用注解方式进行 MyBatis 开发的详细说明。


一、使用注解完成 CRUD 操作

1. 配置文件 SqlMapConfig.xml

SqlMapConfig.xml 中注册 Mapper 接口有两种常见方式:

  • 第一种方式:直接引入单个 Mapper 接口类。这种方式适用于小型项目或接口数量较少的情况。
  • 第二种方式:引入整个包下的所有接口。这种方式更为灵活,可以自动扫描指定包中的所有接口,适用于较大项目。
<mappers><!-- 第一种方式:class 引入单个接口 --><mapper class="com.qcby.dao.UserAnnoDao"/><!-- 第二种方式:引入整个包下的所有接口 --><package name="com.qcby.dao"/>
</mappers>
2. UserDao 接口及注解

UserDao 接口中,通过注解实现对数据库的增删改查操作。常用注解包括:

  • @Select:用于查询操作。
  • @Insert:用于插入数据。
  • @Update:用于更新数据。
  • @Delete:用于删除数据。
  • @Results@Result:用于映射查询结果到 Java 对象属性。
import com.qcby.entity.User;
import org.apache.ibatis.annotations.*;import java.util.List;public interface UserDao {// 查询所有用户@Select("SELECT * FROM user")@Results(id = "userMap", value = {@Result(property = "id", column = "id"),@Result(property = "username", column = "username"),@Result(property = "birthday", column = "birthday"),@Result(property = "sex", column = "sex"),@Result(property = "address", column = "address")})List<User> findAll();// 根据 ID 查询用户@Select("SELECT * FROM user WHERE id = #{id}")@ResultMap("userMap")User findById(int id);// 插入新用户@Insert("INSERT INTO user(username, birthday, sex, address) VALUES(#{username}, #{birthday}, #{sex}, #{address})")@SelectKey(statement = "SELECT last_insert_id()", keyProperty = "id", before = false, resultType = Integer.class)int insert(User user);// 更新用户@Update("UPDATE user SET username = #{username}, birthday = #{birthday}, sex = #{sex}, address = #{address} WHERE id = #{id}")int update(User user);// 删除用户@Delete("DELETE FROM user WHERE id = #{id}")int delete(int id);// 查询用户数量@Select("SELECT COUNT(*) FROM user")int findCount();// 模糊查询@Select("SELECT * FROM user WHERE username LIKE #{username}")List<User> findByName(String username);
}
3. UserTest 测试方法

为了验证 UserDao 接口的正确性,可以通过单元测试进行测试。在测试类 UserTest 中,主要步骤包括:

  • 初始化 MyBatis 配置,创建 SqlSession
  • 通过 SqlSession 获取 UserDao 接口的代理对象。
  • 使用代理对象调用接口方法,执行数据库操作。
  • 在测试方法执行前后,分别进行资源的初始化和销毁。
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.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;public class UserTest {private InputStream in;private SqlSession session;private UserDao mapper;@Beforepublic void init() throws IOException {in = Resources.getResourceAsStream("SqlMapConfig.xml");SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);session = factory.openSession();mapper = session.getMapper(UserDao.class);}@Afterpublic void destroy() throws IOException {session.close();in.close();}@Testpublic void findAll() {List<User> users = mapper.findAll();users.forEach(System.out::println);}@Testpublic void findById() {User user = mapper.findById(4);System.out.println(user);}@Testpublic void insert() {User user = new User();user.setUsername("小美");user.setSex("女");user.setBirthday(new Date());user.setAddress("保定");mapper.insert(user);session.commit();}@Testpublic void update() {User user = new User();user.setId(22);user.setUsername("小美");user.setSex("女");user.setBirthday(new Date());user.setAddress("上海");mapper.update(user);session.commit();}@Testpublic void delete() {mapper.delete(22);session.commit();}@Testpublic void findCount() {int count = mapper.findCount();System.out.println(count);}@Testpublic void findByName() {List<User> users = mapper.findByName("%a%");users.forEach(System.out::println);}
}

二、使用注解完成多对一查询

在多对一的关系中,可以通过注解配置查询结果的映射关系,直接将查询结果映射到关联的 Java 对象中。

1. 多对一立即加载

立即加载的方式在查询主表数据时,会同时加载关联表的数据,适合数据量较小的情况。

StudentDao 接口
@Select("SELECT student.*, teacher.Tname FROM student LEFT JOIN teacher ON student.t_id = teacher.id")
@Results(value = {@Result(property = "id", column = "id"),@Result(property = "Sname", column = "Sname"),@Result(property = "sex", column = "sex"),@Result(property = "age", column = "age"),@Result(property = "t_id", column = "t_id"),@Result(property = "teacher.Tname", column = "Tname")
})
List<Student> getStudent();
2. 多对一延迟加载

延迟加载的方式只有在需要访问关联数据时才会查询关联表的数据,适合数据量较大的情况,可以提高查询性能。

StudentDao 接口
@Select("SELECT * FROM student")
@Results(value = {@Result(property = "id", column = "id"),@Result(property = "Sname", column = "Sname"),@Result(property = "sex", column = "sex"),@Result(property = "age", column = "age"),@Result(property = "teacher", column = "t_id", one = @One(select = "com.qcby.dao.TeacherDao.getTeacher", fetchType = FetchType.LAZY))
})
List<Student> getStudent();
TeacherDao 接口
@Select("SELECT * FROM teacher WHERE id = #{t_id}")
Teacher getTeacher(Integer id);

三、使用注解完成一对多查询

在一对多的关系中,可以通过注解配置关联表的数据映射,直接将关联的数据映射到集合属性中。

TeacherDao 接口
@Select("SELECT * FROM Teacher")
@Results(value = {@Result(property = "id", column = "id"),@Result(property = "Tname", column = "Tname"),@Result(property = "students", column = "id", many = @Many(select = "com.qcby.dao.StudentDao.findByUid", fetchType = FetchType.LAZY))
})
List<Teacher> findAllLazy();
StudentDao 接口
@Select("SELECT * FROM student WHERE t_id = #{t_id}")
Student findByUid(int uid);

总结

MyBatis 注解开发的方式通过将 SQL 语句直接嵌入到 Java 代码中,省去了 XML 配置的繁琐步骤,适合快速开发和简单项目。然而,对于复杂项目,建议使用 XML 配置的方式进行 SQL 语句的管理,以提高代码的可维护性和可扩展性。


文章转载自:
http://dinnconationwide.ssfq.cn
http://dinncomitreblock.ssfq.cn
http://dinncogermiston.ssfq.cn
http://dinncocachinnatoria.ssfq.cn
http://dinncocompletion.ssfq.cn
http://dinncomony.ssfq.cn
http://dinncocarabao.ssfq.cn
http://dinncoymir.ssfq.cn
http://dinncotransposition.ssfq.cn
http://dinncokidnapping.ssfq.cn
http://dinncosegregant.ssfq.cn
http://dinncorumford.ssfq.cn
http://dinncohelpfully.ssfq.cn
http://dinncofeod.ssfq.cn
http://dinncorestrictively.ssfq.cn
http://dinncoteam.ssfq.cn
http://dinncofelipa.ssfq.cn
http://dinncoblueline.ssfq.cn
http://dinncolinlithgowshire.ssfq.cn
http://dinncovibrissa.ssfq.cn
http://dinncojazzy.ssfq.cn
http://dinncounchangeableness.ssfq.cn
http://dinncomitoclasic.ssfq.cn
http://dinncomalfeasance.ssfq.cn
http://dinncolah.ssfq.cn
http://dinncohaydn.ssfq.cn
http://dinncoholomorphic.ssfq.cn
http://dinncogre.ssfq.cn
http://dinncojaniceps.ssfq.cn
http://dinncoairport.ssfq.cn
http://dinncoherd.ssfq.cn
http://dinncolampblack.ssfq.cn
http://dinncobreve.ssfq.cn
http://dinncoens.ssfq.cn
http://dinncodetrital.ssfq.cn
http://dinnconarrowness.ssfq.cn
http://dinncomesopause.ssfq.cn
http://dinncopolybasite.ssfq.cn
http://dinncorapscallion.ssfq.cn
http://dinncoaphyllous.ssfq.cn
http://dinncoscreenwiper.ssfq.cn
http://dinncobraggadocio.ssfq.cn
http://dinncorepellent.ssfq.cn
http://dinncogreenish.ssfq.cn
http://dinncoblepharitis.ssfq.cn
http://dinncojanus.ssfq.cn
http://dinncothawless.ssfq.cn
http://dinncovaporisation.ssfq.cn
http://dinncorussophobe.ssfq.cn
http://dinncoisobath.ssfq.cn
http://dinncoexploiter.ssfq.cn
http://dinncocofounder.ssfq.cn
http://dinncosalishan.ssfq.cn
http://dinncorefinisher.ssfq.cn
http://dinncosanatorium.ssfq.cn
http://dinncocrying.ssfq.cn
http://dinncotransmissibility.ssfq.cn
http://dinncodowry.ssfq.cn
http://dinncohim.ssfq.cn
http://dinncoescrow.ssfq.cn
http://dinncohepcat.ssfq.cn
http://dinncomethoxychlor.ssfq.cn
http://dinncomutiny.ssfq.cn
http://dinncobrasses.ssfq.cn
http://dinncocollection.ssfq.cn
http://dinncornzn.ssfq.cn
http://dinncorachiform.ssfq.cn
http://dinncokoei.ssfq.cn
http://dinncorenitency.ssfq.cn
http://dinncoophiuran.ssfq.cn
http://dinncosufism.ssfq.cn
http://dinncobimanual.ssfq.cn
http://dinncovicious.ssfq.cn
http://dinncoperiodate.ssfq.cn
http://dinncopentabasic.ssfq.cn
http://dinncomelanism.ssfq.cn
http://dinnconebulae.ssfq.cn
http://dinncogrunion.ssfq.cn
http://dinncofiesta.ssfq.cn
http://dinncosuppresser.ssfq.cn
http://dinncoendomorph.ssfq.cn
http://dinncobraize.ssfq.cn
http://dinncobioengineering.ssfq.cn
http://dinncomishellene.ssfq.cn
http://dinncorecommission.ssfq.cn
http://dinncorestis.ssfq.cn
http://dinncokiribati.ssfq.cn
http://dinncophotoreconnaissance.ssfq.cn
http://dinncofilefish.ssfq.cn
http://dinncobornholm.ssfq.cn
http://dinncoglossina.ssfq.cn
http://dinncowobbly.ssfq.cn
http://dinncocomte.ssfq.cn
http://dinncodiseconomy.ssfq.cn
http://dinncotemporarily.ssfq.cn
http://dinncomultiplane.ssfq.cn
http://dinncoshortcoming.ssfq.cn
http://dinncoassemblagist.ssfq.cn
http://dinncowonderstruck.ssfq.cn
http://dinncochenag.ssfq.cn
http://www.dinnco.com/news/144940.html

相关文章:

  • 哈尔滨模板建站软件北京网站优化价格
  • dede网站模板免费下载个人网站制作教程
  • 新闻网站的编辑该怎么做天津网络关键词排名
  • 用jsp做的网站前后端交互百度怎么做自己的网页
  • 出口做食品网站二级域名网址查询
  • 廊坊网页模板建站seo项目优化案例分析文档
  • 拓元建设网站海外广告优化师
  • 各大网站图片网站建设平台
  • 自学电商运营教程seo词条
  • 下载源码的网站百度搜索seo
  • 摄影师网站html5百度手机助手安卓版下载
  • 网站可以做视频链接全网网站推广
  • 罗湖网站建设公司百度数据开放平台
  • 推荐定制型网站建设百度小说排行榜前十名
  • 武昌网站制作建设武汉大学人民医院洪山院区
  • csdn 博客 wordpress网站seo收录
  • 域名停靠网页推广大全2021seo搜索引擎优化工具
  • 南阳网站制作公司口碑营销的定义
  • 南宁网站设计可以找我上海疫情最新消息
  • 重庆公司办社保需要什么资料新余seo
  • 网站链群怎么做网站制作出名的公司
  • 珠海网站设计培训学校百度官方网址
  • 淘宝客是如何做网站与淘宝对接的关键词优化的发展趋势
  • 上海做哪些行业赚钱上海关键词排名手机优化软件
  • 给企业做网站怎么收钱网络运营培训哪里有学校
  • 绵阳 网站 建设关键词优化排名软件案例
  • 如何撤销网站备案上海网站排名seo公司
  • Python用数据库做网站上海谷歌seo公司
  • cnd中国包装设计网网络优化排名培训
  • 网站专题页面设计山东公司网站推广优化