springboot+mybatis整合(入门)
来源:互联网 发布:河南软件 编辑:程序博客网 时间:2024/06/05 13:23
1、使用idea创建springboot项目 选择web、mybatis
或者在pom.xml增加
<!-- mybatis --><dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency><!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
2、编写Mapper
使用到的注解有:@Mapper
@Mapperpublic interface User2Mapper { /** * 2017年7月6日12:31:11 * 根据id获得用户信息 * * @param id * @return User */ @Select("SELECT id,name,age FROM user WHERE id=#{id}") User findById(@Param("id") Integer id); /** * 2017年7月6日12:31:11 * 新增用户信息 * * @param user * @return User */ @Transactional @Insert("INSERT INTO user(name,age) VALUES(#{name},#{age})") @Options(useGeneratedKeys = true,keyProperty = "id") int insertUserInfo(User user); @Update("UPDATE user SET name=#{name},age=#{age} WHERE id=#{id}") int updateUserInfo(User user); @Delete("DELETE FROM user WHERE id=#{id}") int deleteUserInfoById(Integer id);}
3、接口和接口实现
定义一个接口UserService
定义一个接口的实现使用注解@Service声明 Service
@Autowired 将定义好的Mapper注入
@Servicepublic class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; @Autowired User2Mapper user2Mapper; public List<User> findList() { return userMapper.findUserInfo(); } public User findById(Integer id){ return user2Mapper.findById(id); } @Transactional @Override public int insertUserInfo(User user) { try { User user1= new User(); user1.setName("1"); user1.setAge(1); user2Mapper.insertUserInfo(user1); return user2Mapper.insertUserInfo(user); }catch (Exception e){ e.printStackTrace(); } return 0; } }
4、在Controller里面使用@Autowired 调用即可
5、在application.yml增加数据库配置
spring: datasource: url: jdbc:mysql://localhost:3306/hshk username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver
源码托管在:http://git.oschina.net/luweiwei/SpringbootMybatisDemo/tree/master
阅读全文
0 0
- springboot+mybatis整合(入门)
- SpringBoot入门之整合mybatis
- SpringBoot入门-快速整合Mybatis
- SpringBoot学习:SpringBoot整合mybatis
- SpringBoot和Mybatis整合
- springboot 整合mybatis
- springboot+mybatis+dubbo整合
- SpringBoot整合MyBatis
- SpringBoot整合Mybatis
- springBoot整合mybatis
- springboot与mybatis整合
- springboot和mybatis整合
- Springboot整合Mysql+Mybatis
- springboot、mybatis、activemq整合
- SpringBoot整合MyBatis
- SpringBoot 整合mybatis
- SpringBoot + Mybatis 整合demo
- springboot整合mybatis
- 子进程的异步等待方式
- 一起来讨论讨论Java多线程技术-20170710
- 介绍两款高性价比MCU,秒杀低成本市场
- 给UGUI的Button动态添加OnClick监听
- 关于有偿提供拼图响应式后台的通知
- springboot+mybatis整合(入门)
- MTK Android Driver :add a new key
- Storm1.1.0<trident+window+Hbase集成实现词频统计TopN>
- 偶遇暖心的事
- C#获取带有汉字的char[]
- DL:epoch、 iteration和batchsize区别
- 35-第一个只出现一次的字符
- 关于C/C++中typedef的定义与用法总结
- 关于js自执行函数