JAVA中SSM框架的搭建实现CRUD的方法

网友投稿 358 2023-03-11


JAVA中SSM框架的搭建实现CRUD的方法

最近在开发公司的一个系统,系统的框架是用ssm的框架搭建的,当然和这次写博客的不一样,它拥有很多的配置文件,企业级的开发所需要的配置文件是非常繁琐的,今天记录一下一个简单的SSM框架的搭建和实现一个CRUD的操作。

使用的是Maven插件来配置我们需要的jar包,由于操作不多,所以并没有配置很多,要注意自己使用的jdk的版本,选择不同版本号的jdk

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.lr

ssm

war

0.0.1-SNAPSHOT

ssm Maven Webapp

http://maven.apache.org

4.0.2.RELEASE

3.2.8

1.7.12

1.2.17

junit

junit

4.11

test

javax

javaee-api

7.0

com.fasterxml.jackson.core

jackson-databind

2.8.8

org.springframework

spring-test

${srping.version}

org.springframework

spring-core

${srping.version}

org.springframework

spring-oxm

${srping.version}

org.springframework

spring-tx

${srping.version}

org.springframework

spring-jdbc

${srping.version}

org.springframework

spring-aop

${srping.version}

org.springframework

spring-context

${srping.version}

org.springframework

spring-context-support

${srping.version}

org.springframework

spring-expression

${srping.version}

org.springframework

spring-orm

${srping.version}

org.springframework

spring-web

${srping.version}

org.springframework

spring-webmvc

${srping.version}

org.springframework

spring-aspects

${srping.version}

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

1.2.2

mysql

mysql-connector-java

5.1.35

commons-dbcp

commons-dbcp

1.4

jstl

jstl

1.2

taglibs

standard

1.1.2

log4j

log4j

${log4j.version}

org.slf4j

slf4j-api

${slf4j.version}

org.slf4j

slf4j-log4j12

${slf4j.version}

com.alibaba

fastjson

1.2.6

org.codehaus.jackson

jackson-mapper-asl

1.9.13

commons-fileupload

commons-fileupload

1.3.1

commons-io

commons-io

2.4

commons-codec

commons-codec

1.10

net.sourceforge.jexcelapi

jxl

2.6

org.apache.poi

poi

3.8

org.apache.poi

poi-ooxml

3.9

Maven_Project

org.apache.maven.plugins

maven-compiler-plugin

2.3.2

1.7

1.7

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.lr

ssm

war

0.0.1-SNAPSHOT

ssm Maven Webapp

http://maven.apache.org

4.0.2.RELEASE

3.2.8

1.7.12

1.2.17

junit

junit

4.11

test

javax

javaee-api

7.0

com.fasterxml.jackson.core

jackson-databind

2.8.8

org.springframework

spring-test

${srping.version}

org.springframework

spring-core

${srping.version}

org.springframework

spring-oxm

${srping.version}

org.springframework

spring-tx

${srping.version}

org.springframework

spring-jdbc

${srping.version}

org.springframework

spring-aop

${srping.version}

org.springframework

spring-context

${srping.version}

org.springframework

spring-context-support

${srping.version}

org.springframework

spring-expression

${srping.version}

org.springframework

spring-orm

${srping.version}

org.springframework

spring-web

${srping.version}

org.springframework

spring-webmvc

${srping.version}

org.springframework

spring-aspects

${srping.version}

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

1.2.2

mysql

mysql-connector-java

5.1.35

commons-dbcp

commons-dbcp

1.4

jstl

jstl

1.2

taglibs

standard

1.1.2

log4j

log4j

${log4j.version}

org.slf4j

slf4j-api

${slf4j.version}

org.slf4j

slf4j-log4j12

${slf4j.version}

com.alibaba

fastjson

1.2.6

org.codehaus.jackson

jackson-mapper-asl

1.9.13

commons-fileupload

commons-fileupload

1.3.1

commons-io

commons-io

2.4

commons-codec

commons-codec

1.10

net.sourceforge.jexcelapi

jxl

2.6

org.apache.poi

poi

3.8

org.apache.poi

poi-ooxml

3.9

Maven_Project

org.apache.maven.plugins

maven-compiler-plugin

2.3.2

1.7

1.7

然后配置数据库的连接,改成自己的数据库就行了

driver=com.mysql.jdbc.Driver

url=jdbc\:mysql\://locahost\:3306/db

username=root

password=root

maxActive=20

maxIdle=20

minIdle=1

maxWait=60000

配置文件spring-dao.xml,Spring会自动查找其下的类

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

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-3.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-4.0.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

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-3.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-4.0.xsd">

配置文件Spring和Mybatis的整合文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

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-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-4.0.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

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-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-4.0.xsd">

配置事物的文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.1.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.1.xsd">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.1.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.1.xsd">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

配置Springmvc.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

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-3.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-4.0.xsd">

好了!!!需要配置的文件已经配置完成了,足够我们进行一波操作了

接下来是后台的一些代码,这里主要是实体类,dao层,service层,controller层,我把后台的文件一起上传了,mapper.xml文件是实现方法最关键的地方

id, name, password, age

insert into user(name,password,age) values(#{name},#{password},#{age})

delete from user

where id = #{id}

update user set name=#{name},password=#{password},age=#{age} where id=#{id}

select * from user

package com.lr.dao;

import java.util.List;

import com.lr.dto.User;

public interface IUserDao {

//查询用户

public User queryByPrimaryKey(int id);

//删除用户

public int deleteByPrimaryKey(int id);

//更新用户

public int updateByPrimaryKey(User user);

//添加用户

public int addUser(User user);

//查询所有用户

public List findallUser();

}

package com.lr.service;

import java.util.List;

import com.lr.dto.User;

public interface IUserService {

//查询用户

public User getUserById(int userId);

//删除

public void deleteUser(int id);

//更新用户

public void updateUser(User user);

//添加用户

public void addUser(User user);

//查看所有用户

public List findallUser();

}

package com.lr.service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lr.dao.IUserDao;

import com.lr.dto.User;

import com.lr.service.IUserService;

@Service("userService")

public class UserServiceImpl implements IUserService{

@Resource

private IUserDao userdao;

public IUserDao getUserdao() {

return userdao;

}

public void setUserdao(IUserDao userdao) {

this.userdao = userdao;

}

//查询用户

@Override

public User getUserById(int userId) {

return userdao.queryByPrimaryKey(userId);

}

//更新用户

@Override

public void updateUser(User user) {

userdao.updateByPrimaryKey(user);

}

//删除用户

@Override

public void deleteUser(int id) {

userdao.deleteByPrimaryKey(id);

}

//添加用户

@Override

public void addUser(User user) {

userdao.addUser(user);

}

//查询所有用户

@Override

public List findallUser() {

return userdao.findallUser();

}

}

package com.lr.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.lr.dto.User;

import com.lr.service.IUserService;

@Controller

public class UserController {

@Autowired

private IUserService userService;

public IUserService getUserService() {

return userService;

}

public void setUserService(IUserService userService) {

this.userService = userService;

}

//主页面

@RequestMapping("/")

public String userMgr() {

return "showUser";

}

//添加用户

@RequestMapping("/addUser")

@ResponseBody

public void userAdd(User user) {

userService.addUser(user);

}

//删除用户

@RequestMapping("/deleteUser")

@ResponseBody

public void deleteUser(int id){

userService.deleteUser(id);

}

//修改用户

@RequestMapping("/updateUser")

@ResponseBody

public void upadteUser(User user){

userService.updateUser(user);

}

//根据id查找用户

@RequestMapping("/showUser")

@ResponseBody

public User showUser(int id,Model model){

return userService.getUserById(id);

}

//查询所有用户

@RequestMapping("/findallUser")

@ResponseBody

public List findallUser(){

return userService.findallUser();

}

}

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

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-3.1.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-4.0.xsd">

好了!!!需要配置的文件已经配置完成了,足够我们进行一波操作了

接下来是后台的一些代码,这里主要是实体类,dao层,service层,controller层,我把后台的文件一起上传了,mapper.xml文件是实现方法最关键的地方

id, name, password, age

insert into user(name,password,age) values(#{name},#{password},#{age})

parameterType="java.lang.Integer">

select

from user

where id = #{id}

delete from user

where id = #{id}

update user set name=#{name},password=#{password},age=#{age} where id=#{id}

select * from user

package com.lr.dao;

import java.util.List;

import com.lr.dto.User;

public interface IUserDao {

//查询用户

public User queryByPrimaryKey(int id);

//删除用户

public int deleteByPrimaryKey(int id);

//更新用户

public int updateByPrimaryKey(User user);

//添加用户

public int addUser(User user);

//查询所有用户

public List findallUser();

}

package com.lr.service;

import java.util.List;

import com.lr.dto.User;

public interface IUserService {

//查询用户

public User getUserById(int userId);

//删除

public void deleteUser(int id);

//更新用户

public void updateUser(User user);

//添加用户

public void addUser(User user);

//查看所有用户

public List findallUser();

}

package com.lr.service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lr.dao.IUserDao;

import com.lr.dto.User;

import com.lr.service.IUserService;

@Service("userService")

public class UserServiceImpl implements IUserService{

@Resource

private IUserDao userdao;

public IUserDao getUserdao() {

return userdao;

}

public void setUserdao(IUserDao userdao) {

this.userdao = userdao;

}

//查询用户

@Override

public User getUserById(int userId) {

return userdao.queryByPrimaryKey(userId);

}

//更新用户

@Override

public void updateUser(User user) {

userdao.updateByPrimaryKey(user);

}

//删除用户

@Override

public void deleteUser(int id) {

userdao.deleteByPrimaryKey(id);

}

//添加用户

@Override

public void addUser(User user) {

userdao.addUser(user);

}

//查询所有用户

@Override

public List findallUser() {

return userdao.findallUser();

}

}

package com.lr.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.lr.dto.User;

import com.lr.service.IUserService;

@Controller

public class UserController {

@Autowired

private IUserService userService;

public IUserService getUserService() {

return userService;

}

public void setUserService(IUserService userService) {

this.userService = userService;

}

//主页面

@RequestMapping("/")

public String userMgr() {

return "showUser";

}

//添加用户

@RequestMapping("/addUser")

@ResponseBody

public void userAdd(User user) {

userService.addUser(user);

}

//删除用户

@RequestMapping("/deleteUser")

@ResponseBody

public void deleteUser(int id){

userService.deleteUser(id);

}

//修改用户

@RequestMapping("/updateUser")

@ResponseBody

public void upadteUser(User user){

userService.updateUser(user);

}

//根据id查找用户

@RequestMapping("/showUser")

@ResponseBody

public User showUser(int id,Model model){

return userService.getUserById(id);

}

//查询所有用户

@RequestMapping("/findallUser")

@ResponseBody

public List findallUser(){

return userService.findallUser();

}

}

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

姓名:

密码:

年龄:

输入用户id:

id

姓名

密码

年龄

输入用户id:

id

姓名

密码

年龄

输入用户id:

输入用户姓名:

输入密码:

输入用户年龄:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java支付宝接口开发(java支付宝支付接口教程)
下一篇:Java语言描述二叉树的深度和宽度
相关文章

 发表评论

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