时间:2024-01-13 17:36:01 | 来源:网站运营
时间:2024-01-13 17:36:01 来源:网站运营
想要用java做一个网站,小型的就可以了,不知道该学哪些知识?求大佬指点?:SpringBoot+Spring Data JPA 作为自己的项目还是挺方便的。本次我使用的是IDEA编辑器来搭建SpringBoot和Spring Data JPA环境首先,我们在IDEA新建项目的时候,选择Spring Initializr,然后next就行了。
<!--Web必要的--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><!--spring data jpa--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- MySQL的java驅動 --><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId></dependency>
pom文件的完整依赖图如下:#服务端容器的配置server: port: 8887#数据库配置spring: datasource: username: 填写自己的 password: 填写自己的 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://填写自己的机器:3306/填写自己的库?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC # JPA配置 jpa: hibernate: ddl-auto: update show-sql: true # formatSQL得这样写 properties: hibernate: format_sql: true
yml文件完整图如下:package com.zhongfucheng.example.demo.domain;import lombok.Data;import org.hibernate.annotations.GenericGenerator;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import java.io.Serializable;import java.util.Date;/** * 存储用户的信息 * * @author ozc * @version 1.0 */@Entity // jpa的注解,需要加@Table(name = "table_user") // 指定数据库的表名@Data // lombok public class User implements Serializable { @Id @GeneratedValue(generator = "system-uuid") @GenericGenerator(name = "system-uuid", strategy = "uuid") private String userId; private String userNickname; private String userPassword; private String userEmail; private Integer actiState; //激活成功与激活失败常量 public static final int ACTIVATION_SUCCESSFUL = 1; public static final int ACTIVATION_UNSUCCESSFUL = 0; private String actiCode; private Date tokenExptime;}
再补充一句:因为我们有了LomBok的Data注解,并且在IDEA已经下好的LomBok的插件,所以我们可以不用写set、get方法。import com.zhongfucheng.example.demo.domain.User;import org.springframework.data.jpa.repository.JpaRepository;/** * UserDao 操作数据库 * @author ozc * @version 1.0 */public interface UserRepository extends JpaRepository<User, String> {}
UserRepository图如下:// 接口public interface UserService { List<User> getAllUser();}// 实现@Servicepublic class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override public List<User> getAllUser() { return userRepository.findAll(); }}
UserService图如下:@RestControllerpublic class UserController { @Autowired private UserService userService; /** * 得到所有用户 */ @GetMapping(value = "/user", produces = {"application/json;charset=UTF-8"}) public void getAllUser () { List<User> allUser = userService.getAllUser(); for (User user : allUser) { System.out.println(user); } }}
Controller代码图如下:<link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
如无意外的话,我们也能在项目中正常打开页面。<!--freemarker--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId></dependency>
随后,在application.yml文件中,加入freemarker的配置:# freemarker配置 freemarker: suffix: .ftl request-context-attribute: request expose-session-attributes: true content-type: text/html check-template-location: true charset: UTF-8 cache: false template-loader-path: classpath:/templates
这里我简单解释一下:freemarker的文件后缀名为.ftl,程序从/templates路径下加载我们的文件。/** * 得到所有用户 */ @GetMapping(value = "/user", produces = {"application/json;charset=UTF-8"}) public String getAllUser ( Model model) { List<User> allUser = userService.getAllUser(); model.addAttribute("users", allUser); return "/index"; }
图片如下:<#if users?? && (users?size > 0)> <#list users as user> <tr> <td>${user.userId}</td> <td>${user.userNickname}</td> <td>${user.userEmail}</td> <td>${user.actiState}</td> <td><a href="http://localhost:8887/deleteUser?id=${user.userId}">删除</a></td> </tr> </#list> <#else> <h3>还没有任何用户</h3> </#if>
图片如下:/** * 根据ID删除某个用户 */@GetMapping(value = "/deleteUser", produces = {"application/json;charset=UTF-8"})public String deleteUserById (String id,Model model) { userService.deleteUserById(id); return getAllUser(model);}
我们再找几张自己喜欢的图片,简单删除一些不必要模块,替换成我们想要的文字,就可以得到以下的效果了:关键词:指点,知识,小型