第一步:修改pom.xml文件,增加:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
第二步:查看并下载依赖
mvn dependency:tree
第三步:创建dao类
package com.w3c0.app; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository(value = "UserDao") public interface UserDao extends CrudRepository<User, Long> { public User findByEmail(String email); }
第四步:创建service类
package com.w3c0.app; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; import org.springframework.beans.factory.annotation.Autowired; @Service(value = "UserService") public class UserService { @Autowired private UserDao ud; public User getView(Long id) { return ud.findOne(id); } public User findByEmail(String email) { return ud.findByEmail(email); } public User Save(User u) { return ud.save(u); } }
第五步:修改controler类
package com.w3c0.app; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; import org.springframework.beans.factory.annotation.Autowired; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService us; @RequestMapping("/{id}") public User view(@PathVariable("id") Long id) { return us.getView(id); } @RequestMapping("/findbymail") public User findbymail(@RequestParam("email") String email) { return us.findByEmail(email); } @RequestMapping("/save") public User save() { User user = new User(); try{ user.setName("李白");//注:如果保存出现乱码,则是因为java文件没有用utf-8格式存储 }catch(Exception e) { } user.setEmail("lb@qq.com"); return us.Save(user); } }
第六步:创建表
CREATE TABLE IF NOT EXISTS `tf_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `email` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ; INSERT INTO `tf_user` (`id`, `name`, `email`) VALUES (1, '五明', 'xx@111.com'), (2, '刘三', '99@qq.com');
第七步:修改配置文件src/main/resources/application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=UTF-8 spring.datasource.username = root spring.datasource.password = spring.datasource.driverClassName = com.mysql.jdbc.Driver # Specify the DBMS spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager) spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
OK,运行,访问:
http://localhost:8080/user/1 http://localhost:8080/user/2 http://localhost:8080/user/save http://localhost:8080/user/3 http://localhost:8080/user/findbymail?email=lb@qq.com
W3c0.com 提供的内容仅用于培训。我们不保证内容的正确性。通过使用本站内容随之而来的风险与本站无关。W3c0 简体中文版的所有内容仅供测试,对任何法律问题及风险不承担任何责任。 当使用本站时,代表您已接受了本站的使用条款和隐私条款。版权所有,保留一切权利。 鲁ICP备15022115号