vue项目接口域名动态的获取方法
272
2023-03-07
【IntelliJ IDEA】Maven构建自己的第一个Java后台的方法
本文介绍了Maven构建自己的第一个java后台的方法,分享给大家,具体如下:
1.知识后顾
关于如何运用Maven构建自己的第一个项目,上期我已经详细的讲解过了,上篇链接;今天我以SpringMvc,Mybatis框架搭建一个属于你自己的Java后台。
2.必要准备
①IntelliJ IDEA,Maven环境搭好
②熟悉掌握MyBatis,SpringMVC等框架
③mysql数据库的创建
3.整体架构布局
4.具体步骤
①在pom.xml中配置工程要使用的jar包
http://
里面涵盖了Spring,mybatis等一系列jar包,这个过程类似android在build.gradle中添加第三http://方依赖,原理一致。
2.在Resourc目录下建立两个目录分别是:mapper,spring
mapper:mapper是mybatis框架的映射,作用是映射文件在dao层用;这里我创建了一个User.xml映射:
其中红色部分是要引起重视的,最上面的是映射dao层的路径,第二个是返回对象的类型,这里我还是把代码贴出来:
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from user
select * from user where name = #{name} and password = #{password}
spring:主要装载spring的配置文件
1.spring-dao.xml
贴代码:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://sprihttp://ngframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://sprihttp://ngframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">
重视的地方:
连接数据库:
配置全局的mybatis-config以及bean类,mapper下的所有文件
配置dao
2.spring-service.xml
贴代码:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context" xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">
重视地方:
配置service
3.spring-web.xml
贴代码:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc.xsd">
重视地方:
配置controller
5.逻辑实现(以user为例)
①首先在bean中 定义user类
package com.dajiu.bean;
/**
* Created by zhangxing on 2017/4/7.
*/
public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
②然后再dao中定义UserDao接口
package com.dajiu.dao;
import com.dajiu.bean.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* Created by zhangxing on 2017/4/7.
*/
@Repository
public interface UserDao {
List
User getLogin(@Param("name") String name, @Param("password") String password);
}
在user.xml中映射dao层
③接着在service中申明接口
package com.dajiu.service;
import com.dajiu.bean.User;
import java.util.List;
/**
* Created by zhangxing on 2017/4/7.
*/
public interface UserService {
List
User getLogin(String name,String password);
}
④再在service.impl中具体实现接口逻辑
package com.dajiu.service.impl;
import com.dajiu.bean.User;
import com.dajiu.dao.UserDao;
import com.dajiu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by zhangxing on 2017/4/7.
*/
@Service("UserService")
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
public List
return userDao.getAll();
}
public User getLogin(String name, String password) {
return userDao.getLogin(name,password);
}
}
这里的@Autowired相当于新建一个实例
⑤在controller中实现真正的后台调用逻辑
package com.dajiu.controller;
import com.dajiu.bean.User;
import com.dajiu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by zhangxing on 2017/4/7.
*/
@Controller
@RequestMapping("/blog")
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/getUser")
@ResponseBody
public Map
Map map = new HashMap();
List
map.put("user",list);
map.put("status",1);
map.put("success",true);
return map;
}
@RequestMapping("getLogin")
@ResponseBody
public Map
Map map = new HashMap();
User user = userService.getLogin(name,password);
map.put("user",user);
map.put("isLogin",true);
map.put("status",1);
return map;
}
}
这里的@RequestMapping("")表示访问的映射路径,@ResponseBody表示请求结果以json数据格式打印出来,@Controller表示只要访问了上面的根映射路径,就直接调用controller;
现在帮大家理理思路:先请求UserController---->UserService---->UserServiceImpl---->UserDao---->user.xml(mapper)---->bean(user)
6.配置Tomcat服务器
①点击右上角的绿色三角形按钮,点击Edit Configuration
②点击+号,选择Tomcat
③选择local
④填写相关配置
⑤点击Deployment,点击+号,选择Artifact
接着选择第一项,一直enter
这样你的整个工程也就完成了,接下来就是访问了
好了,今天就springMvc,mybatis搭建Java后台的讲解就告一段落了。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~