做网站挣钱吗现在口碑营销有哪些
web/
2025/10/3 21:34:30/
文章来源:
做网站挣钱吗现在,口碑营销有哪些,备案后网站可以改名吗,化妆品网站栏目设计之前写过一篇关于springboot 与 mybatis整合的博文#xff0c;使用了一段时间spring-data-jpa#xff0c;发现那种方式真的是太爽了#xff0c;mybatis的xml的映射配置总觉得有点麻烦。接口定义和映射离散在不同的文件中#xff0c;阅读起来不是很方便。于是#xff0c;准… 之前写过一篇关于springboot 与 mybatis整合的博文使用了一段时间spring-data-jpa发现那种方式真的是太爽了mybatis的xml的映射配置总觉得有点麻烦。接口定义和映射离散在不同的文件中阅读起来不是很方便。于是准备使用mybatis的注解方式实现映射。如果喜欢xml方式的可以看我之前的博文Spring boot Mybatis 整合完整版 个人开源项目 springbootmybatisthymeleafdocker构建的个人站点开源项目集成了个人主页、个人作品、个人博客推荐开源项目 开源的springboot接口文档组件swagger2更多干货 SpringBoot系列目录 源码 请前往文章末端查看 开发环境 开发工具Intellij IDEA 2017.1.3JDK : 1.8.0_101spring boot 版本 1.5.8.RELEASEmaven : 3.3.9拓展 springboot 整合 Mybatis 事务管理开始 1.新建一个springboot项目 添加依赖 2.看一下项目结构 3.完整依赖 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.winterchen/groupIdartifactIdspringboot-mybatis-demo2/artifactIdversion0.0.1-SNAPSHOT/versionpackagingjar/packagingnamespringboot-mybatis-demo2/namedescriptionDemo project for Spring Boot/descriptionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.5.8.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion1.3.1/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project4.配置文件 因为习惯性的喜欢使用yml作为配置文件所以将application.properties替换为application.yml spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mytestusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driver 简单且简洁的完成了基本配置下面看看我们是如何在这个基础下轻松使用Mybatis访问数据库的 使用Mybatis 在Mysql数据库中创建数据表CREATE DATABASE mytest;USE mytest;CREATE TABLE t_user(id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,name VARCHAR(255) NOT NULL ,password VARCHAR(255) NOT NULL ,phone VARCHAR(255) NOT NULL
) ENGINEINNODB AUTO_INCREMENT1000 DEFAULT CHARSETutf8;创建映射对象Userpackage com.winterchen.domain;/*** User实体映射类* Created by Administrator on 2017/11/24.*/public class User {private Integer id;private String name;private String password;private String phone;//省略 get 和 set ...
}创建User映射的操作UserMapper为了后续单元测试验证实现插入和查询操作package com.winterchen.mapper;import com.winterchen.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;/*** User映射类* Created by Administrator on 2017/11/24.*/
Mapper
public interface UserMapper {Select(SELECT * FROM T_USER WHERE PHONE #{phone})User findUserByPhone(Param(phone) String phone);Insert(INSERT INTO T_USER(NAME, PASSWORD, PHONE) VALUES(#{name}, #{password}, #{phone}))int insert(Param(name) String name, Param(password) String password, Param(phone) String phone);} 如果想了解更多Mybatis注解的详细springboot中使用Mybatis注解配置详解 创建springboot 主类package com.winterchen;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class SpringbootMybatisDemo2Application {public static void main(String[] args) {SpringApplication.run(SpringbootMybatisDemo2Application.class, args);}
}创建测试单元: 测试逻辑插入一条name为weinterchen的User然后根据user的phone进行查询并判断user的name是否为winterchen。package com.winterchen;import com.winterchen.domain.User;
import com.winterchen.mapper.UserMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class)
SpringBootTest
public class SpringbootMybatisDemo2ApplicationTests {Autowiredprivate UserMapper userMapper;Testpublic void test(){userMapper.insert(winterchen, 123456, 12345678910);User u userMapper.findUserByPhone(12345678910);Assert.assertEquals(winterchen, u.getName());}}测试结果 说明已经成功了。 **如果出现mapper注入不了的情况请检查版本当前博客的搭建方法只适合1.5.*版本的如果你的版本是2.0以上的版本请参照我的另一篇博客的mybatis的配置springboot2.0整合mybatis ** 事务管理重要 我们在开发企业应用时对于业务人员的一个操作实际是对数据读写的多步操作的结合。由于数据操作在顺序执行的过程中任何一步操作都有可能发生异常异常会导致后续操作无法完成此时由于业务逻辑并未正确的完成之前成功操作数据的并不可靠需要在这种情况下进行回退。 为了测试的成功请把测试的内容进行替换因为之前测试的时候已经将数据生成了重复的数据会对测试的结果有影响 TestTransactionalpublic void test(){userMapper.insert(张三, 123456, 18600000000);int a 1/0;userMapper.insert(李四, 123456, 13500000000);User u userMapper.findUserByPhone(12345678910);Assert.assertEquals(winterchen, u.getName());} 只需要在需要事务管理的方法上添加 Transactional 注解即可然后我们启动测试会发现异常之后数据库中没有产生数据。 如果大家想对springboot事务管理有更加详细的了解欢迎大家查看springboot事务管理详解 源码https://github.com/WinterChenS/springboot-mybatis-demo2/ springboot技术交流群681513531 转载于:https://www.cnblogs.com/winterchens/p/10655558.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/86428.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!