{"id":5246,"date":"2024-09-23T11:01:01","date_gmt":"2024-09-23T03:01:01","guid":{"rendered":""},"modified":"2024-09-23T11:01:01","modified_gmt":"2024-09-23T03:01:01","slug":"spring mybatis\u589e\u5220\u6539\u67e5_mybatis\u6ce8\u89e3\u589e\u5220\u6539\u67e5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/5246.html","title":{"rendered":"spring mybatis\u589e\u5220\u6539\u67e5_mybatis\u6ce8\u89e3\u589e\u5220\u6539\u67e5"},"content":{"rendered":"
\u76ee\u5f55<\/strong><\/p>\n \n \u4e00\u3001\u5b66\u4e60\u524d\u9a71<\/p>\n \u4e8c\u3001\u5185\u5bb9\u4e3b\u4f53<\/p>\n 1\u3001\u6570\u636e\u5e93\u642d\u5efa<\/p>\n 3\u3001\u521b\u5efa\u9879\u76ee\uff08\u8be6\u7ec6\u6b65\u9aa4\u548cspringMVC\u76f8\u540c\uff09\uff0c\u5bfc\u5165\u4f9d\u8d56<\/p>\n 3\u3001\u5177\u4f53\u529f\u80fd\u7684\u5b9e\u73b0<\/p>\n \u5b66\u4e60\u8be5\u5185\u5bb9\u4e4b\u524d\u9996\u5148\u8981\u4e86\u89e3\u600e\u6837\u4f7f\u7528MyBatis\u548cspringMVC\uff0c\u4e0d\u4e86\u89e3\u7684\u53ef\u4ee5\u6d4f\u89c8\u535a\u4e3b\u4e4b\u524d\u7684\u6587\u7ae0<\/p>\n MyBatis:\u4f7f\u7528mybatis\u5b9e\u73b0\u589e\u5220\u6539\u67e5_ZiXiaFan\u7684\u535a\u5ba2-CSDN\u535a\u5ba2<\/p>\n springMVC:idea\u4e2d\u4f7f\u7528maven\u5de5\u7a0b\u642d\u5efaSpringMVC\u6846\u67b6_ZiXiaFan\u7684\u535a\u5ba2-CSDN\u535a\u5ba2<\/p>\n \uff081\uff09\u521b\u5efa\u6570\u636e\u5e93\u5b57\u7b26\u96c6\u7f16\u7801\u6539\u4e3autf8\uff08\u4f8b\uff1astudy\uff09\uff0c\u521b\u5efa\u6570\u636e\u8868\uff0c\u4ee3\u7801\u65b9\u5f0f\uff1a\uff08\u56e0\u4e3a\u7ba1\u7406\u8f6f\u4ef6\u548c\u6570\u636e\u5e93\u4e0d\u540c\uff0c\u7c98\u8d34\u590d\u5236\u53ef\u80fd\u6709\u9519\uff0c\u63a8\u8350\u81ea\u5df1\u624b\u6253\uff09<\/p>\n \u4e5f\u53ef\u4ee5\u624b\u52a8\u65b9\u5f0f\uff0c\u6570\u636e\u5982\u4e0b\uff1a<\/p>\n <\/p>\n \uff082\uff09\u5411\u8868\u4e2d\u6dfb\u52a0\u6570\u636e<\/p>\n \u4ee3\u7801\u65b9\u5f0f\uff1a\uff08\u56e0\u4e3a\u7ba1\u7406\u8f6f\u4ef6\u548c\u6570\u636e\u5e93\u4e0d\u540c\uff0c\u7c98\u8d34\u590d\u5236\u53ef\u80fd\u6709\u9519\uff0c\u63a8\u8350\u81ea\u5df1\u624b\u6253\uff09<\/p>\n \u4e5f\u53ef\u4ee5\u624b\u52a8\u65b9\u5f0f\uff0c\u6570\u636e\u5982\u4e0b <\/p>\n <\/p>\n 1.\u9879\u76ee\u7ed3\u6784\u5982\u4e0b\u56fe<\/p>\n <\/p>\n 2.\u5bfc\u5165\u4f9d\u8d56<\/p>\n 3.\u5728resources\u4e2d\u7f16\u5199mybatis\u7684\u6838\u5fc3\u914d\u7f6e\u6587\u4ef6mybatis-config.xml <\/p>\n 4.\u914d\u7f6eweb.xml<\/p>\n 5.\u521b\u5efaspring\u914d\u7f6e\u6587\u4ef6\uff08\u626b\u63cf\u7ec4\u4ef6\uff0c\u914d\u7f6e\u89c6\u56fe\u89e3\u6790\u5668\uff09<\/p>\n 5.\u5728util\u5305\u4e0b\u7f16\u5199MyBatis\u7684\u5de5\u5177\u7c7b\uff0c\u7528\u6765\u83b7\u53d6sqlSession<\/p>\n <\/p>\n ps\uff1a\u672c\u6587\u7ae0\u4e3b\u8981\u4fa7\u91cd\u8bb2\u89e3MyBatis\u548cspringMVC\uff0c\u4e3a\u4e86\u4e0d\u589e\u52a0\u5b66\u4e60\u96be\u5ea6\u524d\u53f0\u9875\u9762\u6ca1\u6709\u4efb\u4f55\u6e32\u67d3\u6837\u5f0f\u4e5f\u6ca1\u6709\u4f7f\u7528\u524d\u7aef\u6846\u67b6\u548cjs<\/span><\/em><\/p>\n 1.\u524d\u7aef\u9875\u9762<\/p>\n \u9996\u9875\uff08\u6a21\u62df\u767b\u5f55\u9875\u9762\uff09index.html<\/p>\n \u767b\u5f55\u6210\u529f\u7684\u7528\u6237\u4e3b\u9875loginsuccess.html<\/p>\n \u4fee\u6539\u7528\u6237\u4fe1\u606f\u7684\u4e3b\u9875userUpdate.html<\/p>\n \u6dfb\u52a0\u7528\u6237\u7684\u4e3b\u9875addUser.html<\/p>\n 2.bean\u5305\u4e0b\u7684User\u5c01\u88c5\u7c7b<\/p>\n 3.mapper\u5305\u4e0b\u7684mapper\u63a5\u53e3\uff1aUserMaper<\/p>\n 4.\u914d\u7f6emapper\u7684\u914d\u7f6e\u6587\u4ef6UserMapper.xml\u6587\u4ef6\uff08\u7f16\u5199sql\u8bed\u53e5\u7684\u6587\u4ef6\uff09<\/p>\n 5.\u5728service\u5c42\u7f16\u5199\u4e1a\u52a1\u903b\u8f91\u7c7bUserService<\/p>\n 6.controller\u5c42\u7684UserController\uff0c\u63a7\u5236\u9875\u9762\u8df3\u8f6c\uff0c\u524d\u540e\u7aef\u8fde\u63a5<\/p>\n \u5206\u4eab\u603b\u7ed3\uff0c\u5982\u6709\u4fb5\u6743\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"spring mybatis\u589e\u5220\u6539\u67e5_mybatis\u6ce8\u89e3\u589e\u5220\u6539\u67e5\u4e00\u3001\u5b66\u4e60\u524d\u9a71\u5b66\u4e60\u8be5\u5185\u5bb9\u4e4b\u524d\u9996\u5148\u8981\u4e86\u89e3\u600e\u6837\u4f7f\u7528MyBatis\u548cspringMVC\uff0c\u4e0d\u4e86\u89e3\u7684\u53ef...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5246"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=5246"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/5246\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=5246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=5246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=5246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n\u4e00\u3001\u5b66\u4e60\u524d\u9a71<\/h2>\n
\u4e8c\u3001\u5185\u5bb9\u4e3b\u4f53<\/h2>\n
1\u3001\u6570\u636e\u5e93\u642d\u5efa<\/h3>\n
create database 'study' use 'study' create table 'user'( 'id' int (20) not null primary key, 'name' varchar(30) defaukt null, 'pwd' varchar(30) default null )default charset=utf2;<\/code><\/pre>\n
insert into 'user' ('id','name','pwd')values (1,'fzx','010101'), (2,'zs','010101'), (3,'ls','010101')<\/code><\/pre>\n
3\u3001\u521b\u5efa\u9879\u76ee\uff08\u8be6\u7ec6\u6b65\u9aa4\u548cspringMVC\u76f8\u540c\uff09\uff0c\u5bfc\u5165\u4f9d\u8d56<\/h3>\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?> <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\/xsd\/maven-4.0.0.xsd\"> <modelVersion>4.0.0<\/modelVersion> <groupId>com.fan.ssm<\/groupId> <artifactId>ssm<\/artifactId> <version>1.0-SNAPSHOT<\/version> <packaging>war<\/packaging> <dependencies> <!-- SpringMVC --> <dependency> <groupId>org.springframework<\/groupId> <artifactId>spring-webmvc<\/artifactId> <version>5.3.1<\/version> <\/dependency> <!-- \u65e5\u5fd7 --> <dependency> <groupId>ch.qos.logback<\/groupId> <artifactId>logback-classic<\/artifactId> <version>1.2.3<\/version> <\/dependency> <!-- ServletAPI --> <dependency> <groupId>javax.servlet<\/groupId> <artifactId>javax.servlet-api<\/artifactId> <version>3.1.0<\/version> <scope>provided<\/scope> <\/dependency> <!-- Spring5\u548cThymeleaf\u6574\u5408\u5305 --> <dependency> <groupId>org.thymeleaf<\/groupId> <artifactId>thymeleaf-spring5<\/artifactId> <version>3.0.12.RELEASE<\/version> <\/dependency> <!--mybatis--> <!--mysql\u9a71\u52a8--> <dependency> <groupId>mysql<\/groupId> <artifactId>mysql-connector-java<\/artifactId> <version>5.1.46<\/version> <\/dependency> <!--mybatis--> <dependency> <groupId>org.mybatis<\/groupId> <artifactId>mybatis<\/artifactId> <version>3.5.2<\/version> <\/dependency> <!--junit--> <dependency> <groupId>junit<\/groupId> <artifactId>junit<\/artifactId> <version>3.8.2<\/version> <scope>test<\/scope> <\/dependency> <dependency> <groupId>junit<\/groupId> <artifactId>junit<\/artifactId> <version>4.13.2<\/version> <scope>test<\/scope> <\/dependency> <\/dependencies> <!-- \u5728build\u4e2d\u914d\u7f6eresource\uff0c\u6765\u9632\u6b62\u6211\u4eec\u8d44\u6e90\u5bfc\u51fa\u5931\u8d25\u7684\u95ee\u9898 --> <build> <resources> <resource> <directory>src\/main\/resources<\/directory> <includes> <include>**\/*.properties<\/include> <include>**\/*.xml<\/include> <\/includes> <filtering>false<\/filtering> <\/resource> <resource> <directory>src\/main\/resources<\/directory> <includes> <include>**\/*.properties<\/include> <include>**\/*.xml<\/include> <\/includes> <filtering>false<\/filtering> <\/resource> <\/resources> <\/build> <\/project><\/code><\/pre>\n
<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <!DOCTYPE configuration PUBLIC \"-\/\/mybatis.org\/\/DTD Config 3.0\/\/EN\" \"http:\/\/mybatis.org\/dtd\/mybatis-3-config.dtd\"> <!--configuration\u6838\u5fc3\u914d\u7f6e\u6587\u4ef6--> <configuration> <environments default=\"development\"> <environment id=\"development\"> <transactionManager type=\"JDBC\"\/> <dataSource type=\"POOLED\"> <property name=\"driver\" value=\"com.mysql.jdbc.Driver\"\/> <property name=\"url\" value=\"jdbc:mysql:\/\/localhost:3306\/study?characterEncoding=utf8\"\/> <property name=\"username\" value=\"root\"\/> <property name=\"password\" value=\"\"\/> <\/dataSource> <\/environment> <\/environments> <!-- \u6bcf\u4e00\u4e2aMapper.xml\u90fd\u9700\u8981\u5728mybatis\u6838\u5fc3\u914d\u7f6e\u6587\u4ef6\u4e2d\u6ce8\u518c --> <mappers> <mapper resource=\"com\/fan\/ssm\/mapper\/UserMapper.xml\"\/> <\/mappers> <\/configuration><\/code><\/pre>\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?> <web-app xmlns=\"http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/xmlns.jcp.org\/xml\/ns\/javaee http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\/web-app_4_0.xsd\" version=\"4.0\"> <!--\u914d\u7f6e\u7f16\u7801\u8fc7\u6ee4\u5668--> <filter> <filter-name>CharacterEncodingFilter<\/filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter<\/filter-class> <init-param> <param-name>encoding<\/param-name> <param-value>UTF-8<\/param-value> <\/init-param> <init-param> <param-name>forceResponseEncoding<\/param-name> <param-value>true<\/param-value> <\/init-param> <\/filter> <filter-mapping> <filter-name>CharacterEncodingFilter<\/filter-name> <url-pattern>\/*<\/url-pattern> <\/filter-mapping> <!--\u914d\u7f6e\u5904\u7406\u8bf7\u6c42\u65b9\u5f0fput\u548cdelete\u7684HiddenHttpMethodFilter--> <filter> <filter-name>HiddenHttpMethodFilter<\/filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter<\/filter-class> <\/filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter<\/filter-name> <url-pattern>\/*<\/url-pattern> <\/filter-mapping> <!--springMVC\u524d\u7aef\u63a7\u5236\u5668DispatcherServlet--> <servlet> <servlet-name>DispatcherServlet<\/servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet<\/servlet-class> <init-param> <param-name>contextConfigLocation<\/param-name> <param-value>classpath:springMVC.xml<\/param-value> <\/init-param> <load-on-startup>1<\/load-on-startup> <\/servlet> <servlet-mapping> <servlet-name>DispatcherServlet<\/servlet-name> <url-pattern>\/<\/url-pattern> <\/servlet-mapping> <\/web-app><\/code><\/pre>\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\n xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n xmlns:context=\"http:\/\/www.springframework.org\/schema\/context\"\n xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd http:\/\/www.springframework.org\/schema\/context https:\/\/www.springframework.org\/schema\/context\/spring-context.xsd\">\n\n <!--\u626b\u63cf\u7ec4\u4ef6-->\n <context:component-scan base-package=\"com.fan.ssm.controller\"><\/context:component-scan>\n\n <!-- \u914d\u7f6eThymeleaf\u89c6\u56fe\u89e3\u6790\u5668 -->\n <bean id=\"viewResolver\" class=\"org.thymeleaf.spring5.view.ThymeleafViewResolver\">\n <property name=\"order\" value=\"1\"\/>\n <property name=\"characterEncoding\" value=\"UTF-8\"\/>\n <property name=\"templateEngine\">\n <bean class=\"org.thymeleaf.spring5.SpringTemplateEngine\">\n <property name=\"templateResolver\">\n <bean class=\"org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver\">\n\n <!-- \u89c6\u56fe\u524d\u7f00 -->\n <property name=\"prefix\" value=\"\/WEB-INF\/templates\/\"\/>\n\n <!-- \u89c6\u56fe\u540e\u7f00 -->\n <property name=\"suffix\" value=\".html\"\/>\n <property name=\"templateMode\" value=\"HTML5\"\/>\n <property name=\"characterEncoding\" value=\"UTF-8\" \/>\n <\/bean>\n <\/property>\n <\/bean>\n <\/property>\n <\/bean>\n\n\n<\/beans><\/code><\/pre>\n
package com.fan.ssm.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; \/\/sqlSessionFactory --> sqlSession public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { \/\/\u4f7f\u7528mybatis\u7684\u7b2c\u4e00\u6b65\uff1a\u83b7\u53d6sqlSessionFactory\u5bf9\u8c61 String resource = \"mybatis-config.xml\"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } \/\/\u65e2\u7136\u6709\u4e86 SqlSessionFactory\uff0c\u987e\u540d\u601d\u4e49\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u4e2d\u83b7\u5f97 SqlSession \u7684\u5b9e\u4f8b\u3002 \/\/ SqlSession \u63d0\u4f9b\u4e86\u5728\u6570\u636e\u5e93\u6267\u884c SQL \u547d\u4ee4\u6240\u9700\u7684\u6240\u6709\u65b9\u6cd5\u3002 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } } <\/code><\/pre>\n
3\u3001\u5177\u4f53\u529f\u80fd\u7684\u5b9e\u73b0<\/h3>\n
<!DOCTYPE html>\n<html lang=\"en\" xmlns:th=\"http:\/\/www.thymeleaf.org\">\n<head>\n <meta charset=\"UTF-8\">\n <title>\u767b\u5f55<\/title>\n<\/head>\n<body>\n<form th:action=\"@{\/login}\" method=\"post\">\n <table border=\"0\">\n <tr>\n <td>\u7528\u6237\u540d<\/td>\n <td><input type=\"text\" name=\"name\"><\/td>\n <\/tr>\n <tr>\n <td>\u5bc6\u7801<\/td>\n <td><input type=\"password\" name=\"pwd\"><\/td>\n <\/tr>\n <tr>\n <td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"\u767b\u5f55\"><\/td>\n <\/tr>\n <tr>\n <td colspan=\"2\" align=\"right\"><a th:href=\"@{\/raddUser}\">\u65b0\u7528\u6237\u6ce8\u518c<\/a><\/td>\n <\/tr>\n <\/table>\n<\/form>\n<\/body>\n<\/html><\/code><\/pre>\n
<!DOCTYPE html>\n<html lang=\"en\" xmlns:th=\"http:\/\/www.thymeleaf.org\">\n<head>\n <meta charset=\"UTF-8\">\n <title>\u7528\u6237\u4e3b\u9875<\/title>\n<\/head>\n<body>\n <table border=\"1\">\n <tr th:each=\"login:${user}\">\n <td><span th:text=\"${login.name}\"><\/span><\/td>\n <td><span th:text=\"${login.pwd}\"><\/span><\/td>\n <td>\n <a th:href=\"@{\/update(id=${login.id})}\">\u4fee\u6539<\/a>\n <a th:href=\"@{\/delete(id=${login.id})}\">\u5220\u9664<\/a>\n <\/td>\n <\/tr>\n <\/table>\n\n<\/body>\n<\/html><\/code><\/pre>\n
<!DOCTYPE html>\n<html lang=\"en\" xmlns:th=\"http:\/\/www.thymeleaf.org\">\n<head>\n <meta charset=\"UTF-8\">\n <title>\u4fee\u6539\u7528\u6237<\/title>\n<\/head>\n<body>\n<form th:action=\"@{\/login}\" method=\"post\">\n <input type=\"hidden\" name=\"id\" th:value=\"${user.id}\">\n <table border=\"1\">\n <tr>\n <td>\u7528\u6237\u540d<\/td>\n <td><input type=\"text\" name=\"name\" th:value=\"${user.name}\"><\/td>\n <\/tr>\n <tr>\n <td>\u5bc6\u7801<\/td>\n <td><input type=\"text\" name=\"pwd\" th:value=\"${user.pwd}\"><\/td>\n <\/tr>\n <tr>\n <td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"\u63d0\u4ea4\"><\/td>\n <\/tr>\n <\/table>\n<\/form>\n<\/body>\n<\/html><\/code><\/pre>\n
<!DOCTYPE html>\n<html lang=\"en\" xmlns:th=\"http:\/\/www.thymeleaf.org\">\n<head>\n <meta charset=\"UTF-8\">\n <title>\u65b0\u7528\u6237\u6ce8\u518c<\/title>\n<\/head>\n<body>\n<form th:action=\"@{\/addUser}\" method=\"post\">\n <table border=\"0\">\n <tr>\n <td>\u7528\u6237\u540d<\/td>\n <td><input type=\"text\" name=\"name\"><\/td>\n <\/tr>\n <tr>\n <td>\u5bc6\u7801<\/td>\n <td><input type=\"password\" name=\"pwd\"><\/td>\n <\/tr>\n <tr>\n <td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"\u6ce8\u518c\"><\/td>\n <\/tr>\n <\/table>\n<\/form>\n<\/body>\n<\/html><\/code><\/pre>\n
package com.fan.ssm.bean; public class User { private Integer id; private String name; private String pwd; public User() { } public User(Integer id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return \"User{\" + \"id=\" + id + \", name='\" + name + '\\'' + \", pwd='\" + pwd + '\\'' + '}'; } } <\/code><\/pre>\n
package com.fan.ssm.mapper; import com.fan.ssm.bean.User; import java.util.List; public interface UserMapper { \/*\u6839\u636e\u7528\u6237\u540d\u5bc6\u7801\u67e5\u8be2\u5355\u4e2auser*\/ public User Login(User user); \/\/\u67e5\u8be2\u6240\u6709\u7528\u6237 public List<User> queryAll(); \/\/\u6839\u636eid\u67e5\u8be2\u7528\u6237 public User queryOneById(Integer id); \/\/\u6839\u636eid\u4fee\u6539\u7528\u6237 public int updateUserById(User user); \/\/\u6839\u636eid\u5220\u9664\u7528\u6237 public int deleteUserById(Integer id); \/\/\u6dfb\u52a0\u7528\u6237 public int addUser(User user); } <\/code><\/pre>\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\"> <!--namespace\u7ed1\u5b9a\u4e00\u4e2a\u5bf9\u5e94\u7684dao\/mapper\u63a5\u53e3--> <mapper namespace=\"com.fan.ssm.mapper.UserMapper\"> <select id=\"Login\" resultType=\"com.fan.ssm.bean.User\" parameterType=\"com.fan.ssm.bean.User\"> select * from user where name=#{name} and pwd=#{pwd} <\/select> <select id=\"queryAll\" resultType=\"com.fan.ssm.bean.User\"> select * from user <\/select> <select id=\"queryOneById\" resultType=\"com.fan.ssm.bean.User\"> select * from user where id=#{id} <\/select> <update id=\"updateUserById\" parameterType=\"com.fan.ssm.bean.User\"> update user set name=#{name} , pwd=#{pwd} where id=#{id} <\/update> <delete id=\"deleteUserById\" parameterType=\"int\"> delete from user where id=#{id} <\/delete> <insert id=\"addUser\" parameterType=\"com.fan.ssm.bean.User\"> insert into user (name,pwd) values (#{name},#{pwd}); <\/insert> <\/mapper><\/code><\/pre>\n
package com.fan.ssm.service; import com.fan.ssm.bean.User; import com.fan.ssm.mapper.UserMapper; import com.fan.ssm.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import java.util.ArrayList; import java.util.List; public class UserService { \/\/\u767b\u5f55 \u6839\u636ename\u548cpwd\u67e5\u8be2\u4e00\u6761 public User login(User user){ SqlSession sqlSession= MybatisUtils.getSqlSession(); UserMapper mapper=sqlSession.getMapper(UserMapper.class); User login = mapper.Login(user); sqlSession.close(); return login; } \/\/\u67e5\u8be2\u6240\u6709\u6570\u636e public List<User> queryAll(){ SqlSession sqlSession= MybatisUtils.getSqlSession(); UserMapper mapper=sqlSession.getMapper(UserMapper.class); List<User> list = new ArrayList<User>(); list =mapper.queryAll(); sqlSession.close(); return list; } \/\/\u6839\u636eid\u67e5\u8be2\u4e00\u6761 public User queryOneById(Integer id){ SqlSession sqlSession= MybatisUtils.getSqlSession(); UserMapper mapper=sqlSession.getMapper(UserMapper.class); User user=mapper.queryOneById(id); sqlSession.close(); return user; } \/\/\u6839\u636eid\u4fee\u6539\u7528\u6237 public int updateUserById(User user){ Integer res=0; SqlSession sqlSession= MybatisUtils.getSqlSession(); UserMapper mapper=sqlSession.getMapper(UserMapper.class); res=mapper.updateUserById(user); sqlSession.commit(); sqlSession.close(); return res; } \/\/\u6839\u636eid\u5220\u9664\u7528\u6237 public int deleteUserById(Integer id){ Integer res=0; SqlSession sqlSession= MybatisUtils.getSqlSession(); UserMapper mapper=sqlSession.getMapper(UserMapper.class); res=mapper.deleteUserById(id); sqlSession.commit(); sqlSession.close(); return res; } \/\/\u7528\u6237\u6ce8\u518c public int addUser(User user){ Integer res=0; SqlSession sqlSession= MybatisUtils.getSqlSession(); UserMapper mapper=sqlSession.getMapper(UserMapper.class); res=mapper.addUser(user); sqlSession.commit(); sqlSession.close(); return res; } } <\/code><\/pre>\n
package com.fan.ssm.controller; import com.fan.ssm.bean.User; import com.fan.ssm.service.UserService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import java.util.ArrayList; import java.util.List; @Controller public class UserController { \/\/\u767b\u5f55\u548c\u67e5\u8be2,\u6839\u636euser\u7684id\u662f\u5426\u4e3a\u7a7a\u5224\u65ad\u662f\u6839\u636eid\u67e5\u8be2\u8fd8\u662f\u6839\u636e\u7528\u6237\u540d\u5bc6\u7801\u67e5\u8be2 @RequestMapping(\"\/login\") public String login(User user , Model model){ System.out.println(user); UserService service=new UserService(); if(user.getId()==null){ User login = service.login(user); if (login != null){ List<User> userList=new ArrayList<User>(); userList = service.queryAll(); model.addAttribute(\"user\",userList); return \"loginsuccess\"; } else{ model.addAttribute(\"failure\",\"failure\"); return \"index\"; } }else { int res=service.updateUserById(user); if(res>0){ List<User> userList=new ArrayList<User>(); userList = service.queryAll(); model.addAttribute(\"user\",userList); return \"loginsuccess\"; } else { System.out.println(\"\u4fee\u6539\u5931\u8d25\"); } } return \"loginsuccess\"; } \/\/\u4fee\u6539\u7528\u6237 @RequestMapping(\"\/update\") public String uupdate(@RequestParam(value = \"id\") Integer id, Model model){ UserService service=new UserService(); User user = service.queryOneById(id); model.addAttribute(\"user\",user); return \"userUpdate\"; } \/\/\u5220\u9664\u7528\u6237 @RequestMapping(\"\/delete\") public String delete(@RequestParam(value = \"id\") Integer id, Model model){ UserService service=new UserService(); Integer res= service.deleteUserById(id); if(res>0){ List<User> userList=new ArrayList<User>(); userList = service.queryAll(); model.addAttribute(\"user\",userList); return \"loginsuccess\"; }else{ System.out.println(\"\u5220\u9664\u5931\u8d25\"); } return \"loginsuccess\"; } \/\/\u7528\u6237\u6ce8\u518c @RequestMapping(\"\/addUser\") public String addUser(User user){ UserService service=new UserService(); Integer res= service.addUser(user); if(res>0){ return \"index\"; }else { System.out.println(\"\u6ce8\u518c\u5931\u8d25\"); } return \"index\"; } @RequestMapping(\"\/raddUser\") public String raddUser(){ return \"addUser\"; } @RequestMapping(\"\/\") public String index(){ return \"index\"; } } <\/code><\/pre>\n