【IntelliJ IDEA】Maven构建自己的第一个Java后台的方法

网友投稿 256 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包

4.0.0

war

yakei

com.yakei

yakei

1.0-SNAPSHOT

junit

junit

4.11

test

org.slf4j

slf4j-api

1.7.12

ch.qos.logback

logback-core

1.1.1

ch.qos.logback

logback-classic

1.1.1

mysql

mysql-connector-java

5.1.36

runtime

c3p0

c3p0

0.9.1.1

org.mybatis

mybatis

3.3.0

org.mybatis

mybatis-spring

1.2.3

taglibs

standard

1.1.2

jstl

jstl

1.2

com.fasterxml.jackson.core

jackson-databind

2.5.4

javax.servlet

javax.servlet-api

3.1.0

org.springframework

spring-core

4.1.7.RELEASE

http://org.springframework

spring-beans

4.1.7.RELEASE

org.springframework

spring-context

4.1.7.RELEASE

org.springframework

spring-jdbc

4.1.7.RELEASE

org.springframework

spring-tx

4.1.7.RELEASE

org.springframework

spring-web

4.1.7.RELEASE

org.springframework

spring-webmvc

4.1.7.RELEASE

org.springframework

spring-test

4.1.7.RELEASE

里面涵盖了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 getAll();

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 getAll();

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 getAll() {

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 getUser(){

Map map = new HashMap();

List list = userService.getAll();

map.put("user",list);

map.put("status",1);

map.put("success",true);

return map;

}

@RequestMapping("getLogin")

@ResponseBody

public Map getLogin(String name,String password){

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小时内删除侵权内容。

上一篇:api接口文档长什么样(api接口文档生成工具)
下一篇:kong api管理(kong admin api)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~