這期內(nèi)容當中小編將會給大家?guī)碛嘘P這么早JAVA中利用SSM框架搭建一個CRUD,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)長期為上1000家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為聞喜企業(yè)提供專業(yè)的成都網(wǎng)站建設、成都網(wǎng)站制作,聞喜網(wǎng)站改版等技術服務。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
使用的是Maven插件來配置我們需要的jar包,由于操作不多,所以并沒有配置很多,要注意自己使用的jdk的版本,選擇不同版本號的jdk
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lr</groupId> <artifactId>ssm</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ssm Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 用來設置版本號 --> <properties> <srping.version>4.0.2.RELEASE</srping.version> <mybatis.version>3.2.8</mybatis.version> <slf4j.version>1.7.12</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <!-- 用到的jar包 --> <dependencies> <!-- 單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- 表示開發(fā)的時候引入,發(fā)布的時候不會加載此包 --> <scope>test</scope> </dependency> <!-- java ee包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.8</version> </dependency> <!-- spring框架包 start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${srping.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${srping.version}</version> </dependency> <!-- spring框架包 end --> <!-- mybatis框架包 start --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- mybatis框架包 end --> <!-- 數(shù)據(jù)庫驅(qū)動 --> <dependency> <groupId>MySQL</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <!-- 導入dbcp的jar包,用來在applicationContext.xml中配置數(shù)據(jù)庫 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- jstl標簽類 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log END --> <!-- Json --> <!-- 格式化對象,方便輸出日志 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- 上傳組件包 start --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <!-- 上傳組件包 end --> <!-- AL相關添加 --> <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.8</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> <!-- AL相關添加 --> </dependencies> <build> <finalName>Maven_Project</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
然后配置數(shù)據(jù)庫的連接,改成自己的數(shù)據(jù)庫就行了
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會自動查找其下的類
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--basePackage指定要掃描的包,在此包之下的映射器都會被搜索到。 可指定多個包,包與包之間用逗號或分號分隔--> <property name="basePackage" value="com.lr.dao" /> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
配置文件Spring和Mybatis的整合文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 引入配置文件 --> <context:property-placeholder location="classpath:/jdbc.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="root" /> <!-- 初始化連接大小 --> <property name="initialSize" value="3" /> <!-- 連接池最大數(shù)量 --> <property name="maxActive" value="20" /> <!-- 連接池最大空閑 --> <property name="maxIdle" value="20" /> <!-- 連接池最小空閑 --> <property name="minIdle" value="1" /> <!-- 獲取連接最大等待時間 --> <property name="maxWait" value="60000" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/lr/mapper/*.xml"></property> </bean> </beans>
配置事物的文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置參與事務的類 --> <aop:config> <aop:pointcut id="allServiceMethod" expression="execution(* com.lr.service.*.*(..))"/> <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" /> </aop:config> <!-- 使用聲明方式配置事務 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> </tx:attributes> </tx:advice> </beans>
配置Springmvc.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自動掃描 --> <context:component-scan base-package="com.lr.controller" /> <mvc:annotation-driven /> <mvc:default-servlet-handler/> <!-- 定義跳轉的文件的前后綴 ,視圖模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>
好了?。?!需要配置的文件已經(jīng)配置完成了,足夠我們進行一波操作了
接下來是后臺的一些代碼,這里主要是實體類,dao層,service層,controller層,我把后臺的文件一起上傳了,mapper.xml文件是實現(xiàn)方法最關鍵的地方
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.lr.dao.IUserDao"> <resultMap id="BaseResultMap" type="com.lr.dto.User"> <result column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="age" property="age" jdbcType="INTEGER" /> </resultMap> <sql id="Base_Column_List"> id, name, password, age </sql> <!-- 增加用戶 --> <insert id="addUser" parameterType="com.lr.dto.User"> insert into user(name,password,age) values(#{name},#{password},#{age}) </insert> <!-- 查詢用戶--> <select id="queryByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer"> select <include refid="Base_Column_List" /> from user where id = #{id} </select> <!-- 刪除用戶 --> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from user where id = #{id} </delete> <!-- 更新用戶 --> <update id="updateByPrimaryKey" parameterType="com.lr.dto.User" > update user set name=#{name},password=#{password},age=#{age} where id=#{id} </update> <select id="findallUser" resultType="com.lr.dto.User"> select * from user </select> </mapper>
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<User> 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<User> 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<User> 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); } //根據(jù)id查找用戶 @RequestMapping("/showUser") @ResponseBody public User showUser(int id,Model model){ return userService.getUserById(id); } //查詢所有用戶 @RequestMapping("/findallUser") @ResponseBody public List<User> findallUser(){ return userService.findallUser(); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"><script type="text/javascript" src="<%= application.getContextPath() %>/js/jquery-1.12.4.min.js"></script> <script type="text/javascript"> //添加用戶 $(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+="<tr><th>"+msg[i].id+"</th><th>"+msg[i].name+"</th><th>" +msg[i].password+"</th><th>"+msg[i].age+"</th><tr>" } $("#findall").append(str); } }); }); }); //根據(jù)id查找一個用戶 $(function(){ $("#find").click(function(){ $.ajax({ type:"POST", data:{id:$("#findid").val()}, dataType:"json", url:"/ssm/showUser", success:function(user){ var str=""; str+="<tr><th>"+user.id+"</th><th>"+user.name+"</th><th>" +user.password+"</th><th>"+user.age+"</th><tr>" $("#finduserbyid").append(str); } }) }) }) //根據(jù)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("修改成功!") }); } </script> <title>用戶管理</title> </head> <body> <div> <p>姓名:<input type="text" id="txtName"></p> <p>密碼:<input type="password" id="txtPassword"></p> <p>年齡:<input type="text" id="txtAge"></p> <p><button id="add">添加</button></p> </div> <hr /> <div> <p>輸入用戶id:<input type="text" id="deleteid"></p> <p><button id="delete">刪除</button></p> </div> <hr /> <div><p><button id="findalluser">查詢所有</button></p></div> <div> <table width="300" border="1" > <thead id="findall"> <tr> <th width="50">id</th> <th width="50">姓名</th> <th width="50">密碼</th> <th width="50">年齡</th> </tr> </thead> </table> </div> <hr /> <div> <p>輸入用戶id:<input type="text" id="findid"></p> <p><button id="find">查詢</button></p> </div> <div> <table width="300" border="1" > <thead id="finduserbyid"> <tr> <th width="50">id</th> <th width="50">姓名</th> <th width="50">密碼</th> <th width="50">年齡</th> </tr> </thead> </table> </div> <hr /> <div> <p>輸入用戶id:<input type="text" id="updateid"></p> <p>輸入用戶姓名:<input type="text" id="updatename"></p> <p>輸入密碼:<input type="password" id="updatepassword"></p> <p>輸入用戶年齡:<input type="password" id="updateage"></p> <p><button id="update">修改</button></p> </div> </body> </html>
上述就是小編為大家分享的這么早JAVA中利用SSM框架搭建一個CRUD了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:這么早JAVA中利用SSM框架搭建一個CRUD
文章來源:http://jinyejixie.com/article36/gdjhpg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設計公司、網(wǎng)站維護、App開發(fā)、網(wǎng)站導航、建站公司、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)