vue项目接口域名动态的获取方法
366
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">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
然后配置数据库的连接,改成自己的数据库就行了
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 } 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 } 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 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 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
}
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
}
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
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
return userService.findallUser();
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
//添加用户
$(function(){
$("#add").on("click", addNewUser);
})
function addNewUser(){
var name = $.trim($("#txtName").val());
var password = $.trim($("#txtPassword").val());
var age = $.trim($("#txtAge").val());
$.post("/ssm/addUser", {"name": name, "password": password, "age": age}, function(){
alert("添加成功!")
});
}
//删除用户
$(function(){
$("#delete").on("click",deleteUser);
})
function deleteUser(){
var id=$.trim($("#deleteid").val());
$.get("/ssm/deleteUser",{"id":id},function(){
alert("删除成功!")
});
}
//查询所有用户
$(function(){
$("#findalluser").click(function(){
$.ajax({
type:"POST",
dataType:"json",
url:"/ssm/findallUser",
success:function(msg){
var str="";
for(i in msg){
str+="
+msg[i].password+"
}
$("#findall").append(str);
}
});
});
});
//根据id查找一个用户
$(function(){
$("#find").click(function(){
$.ajax({
type:"POST",
data:{id:$("#findid").val()},
dataType:"json",
url:"/ssm/showUser",
success:function(user){
var str="";
str+="
+user.password+"
$("#finduserbyid").append(str);
}
})
})
})
//根据id修改用户信息
$(function(){
$("#update").on("click",updateUser);
})
function updateUser(){
alert($.trim($("#updateid").val()))
alert($.trim($("#updatename").val()))
alert($.trim($("#updatepassword").val()))
alert($.trim($("#updateage").val()))
var id=$.trim($("#updateid").val());
var name=$.trim($("#updatename").val());
var password=$.trim($("#updatepassword").val());
var age=$.trim($("#updateage").val());
$.post("/ssm/updateUser",{"id":id,"name":name,"password":password,"age":age},function(){
alert("修改成功!")
});
}
姓名:
密码:
年龄:
输入用户id:
id 姓名 密码 年龄
输入用户id:
id 姓名 密码 年龄
输入用户id:
输入用户姓名:
输入密码:
输入用户年龄:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~