One. MySQL database is used. Create a new database named task and a new table named student in the task database. Add data to the table as appropriate. The SQL statements and tables for creating tables and adding data are as follows.
--Create table DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `sid` varchar(40) NOT NULL DEFAULT '' COMMENT 'UUID,Unique identification', `id` int(10) DEFAULT NULL COMMENT 'Student number', `name` varchar(20) DEFAULT NULL COMMENT 'Student name', `password` varchar(20) DEFAULT NULL COMMENT 'Student password', `age` int(3) unsigned DEFAULT NULL COMMENT 'Student age', `sex` varchar(2) DEFAULT NULL COMMENT 'Student gender', PRIMARY KEY (`sid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; --Add data to table INSERT INTO `student` VALUES ('9577f4badff2443e9e67bcc63b640341', '1005', 'KID', '12345', '20', 'female'); INSERT INTO `student` VALUES ('9577f4badff2443e9e67bcc63b640342', '1001', 'Zhang San', '1235', '12', 'female'); INSERT INTO `student` VALUES ('c93bbe3c69ed4cbeb07a2a06ccb97612', '1002', 'Li Si', '1235', '30', 'male'); INSERT INTO `student` VALUES ('db7d44de97e1418d95fd73a77b9a9b0c', '1003', 'Wang Wu', '1235', '12', 'female'); INSERT INTO `student` VALUES ('fjakslfk349rhrksar4', '1004', 'Zhao Liu', '1235', '25', 'male');
2, Create Maven project in ideal
1.new project
Note: pay attention to the version number when building the project. Errors may be reported due to different version numbers in the follow-up!
2. Set package name
3. Introduce Mybatis (mapping)
Note: MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures, and advanced mapping. MyBatis eliminates almost all JDBC code and the work of setting parameters and obtaining result sets. MyBatis can configure and map primitive types, interfaces and Java POJO s (Plain Old Java Objects) to records in the database through simple XML or annotations
(1) Failure to obtain file resources may occur. You can directly copy the following code into the pom.xml configuration file of the project.
<!--stay build Medium configuration resources,To prevent resource export failure--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
(2) You need to create a mybatis-config.xml file in the outlets package. The file name can be other, but it is best to match the official file name; Copy and paste the official configuration file code into your newly created configuration file.
(3) Configure your own database connection data in < mapper > < / mapper >. Provides information about connecting to the database.
(4) Create a config.properties resource file in the Resources folder.
(5) In the newly created config.properties resource file, add your own database information.
3, Code implementation addition, deletion, modification and query
The specification uses MVC Architecture (a new class is written in which folder in which directory? At this time, the solution is: a schema is needed to standardize which class should be written.) the controller stores the control layer code, the dao package stores the database tool class, and the pojo package stores the Student instance class, The service package stores the business interface and the corresponding configuration file.
(1) Control layer controller code
package com.kid.controller; import com.kid.dao.MyBatisUtil; import com.kid.pojo.Student; import com.kid.service.StudentService; import org.apache.ibatis.session.SqlSession; import java.util.List; public class StudentController { /** * Overload, query all student information without parameters * * @return Returns a list of information for all students */ public List<Student> selectStudent() { return getStudentAll(); } /** * Overload, select and call the method to find students according to the parameters * * @param object Find the value of the parameter required by the student * @param str Find the type of parameters required by students, Sid, Id, Name, Sex, Age * @return */ public List<Student> selectStudent(Object object, String str) { List<Student> list = null; if (str == null) { list = getStudentAll(); } else { switch (str) { case "sid": case "Sid": list = getStudentBySid((String) object); break; case "id": case "Id": list = getStudentById((int) object); break; case "name": case "Name": list = getStudentByName((String) object); break; case "age": case "Age": list = getStudentByAge((int) object); break; case "sex": case "Sex": list = getStudentBySex((String) object); break; } } return list; } /** * Find all student information * * @return Returns a list of information for all students */ public List<Student> getStudentAll() { List<Student> list = null; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); list = StudentService.getStudentAll(); sqlSession.close(); return list; } /** * Find student information according to Sid * * @param sid Student unique ID * @return Returns the list of student information that meets the query criteria */ public List<Student> getStudentBySid(String sid) { List<Student> list = null; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); list = StudentService.getStudentBySid(sid); sqlSession.close(); return list; } /** * Query student information according to student Id and student number * * @param id Student number * @return Returns the list of student information that meets the query criteria */ public List<Student> getStudentById(int id) { List<Student> list = null; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); list = StudentService.getStudentById(id); sqlSession.close(); return list; } /** * Query student information according to student name * * @param name Student name * @return Returns the list of student information that meets the query criteria */ public List<Student> getStudentByName(String name) { List<Student> list = null; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); list = StudentService.getStudentByName(name); sqlSession.close(); return list; } /** * Query student information according to student age * * @param age Student age * @return Returns the list of student information that meets the query criteria */ public List<Student> getStudentByAge(int age) { List<Student> list = null; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); list = StudentService.getStudentByAge(age); sqlSession.close(); return list; } /** * Query student information according to student gender * * @param sex Student gender * @return Returns the list of student information that meets the query criteria */ public List<Student> getStudentBySex(String sex) { List<Student> list = null; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); list = StudentService.getStudentBySex(sex); sqlSession.close(); return list; } /** * Add student information according to the provided Student object * * @param student Student object * @return If the insertion is successful, it returns true, otherwise it returns false */ public boolean insertStudent(Student student) { boolean result = false; SqlSession session = MyBatisUtil.getSqlSession(); StudentService StudentService = session.getMapper(StudentService.class); result = StudentService.insertStudent(student) > 0 ? true : false; session.close(); return result; } /** * Select and call the method to delete student information according to the parameters * * @param object Required parameter value * @param str Delete student basis according to Sid, Id, Name, Sex, Age * @return True will be returned if the deletion is successful, otherwise false will be returned */ public boolean deleteStudent(Object object, String str) { boolean flag = false; switch (str) { case "sid": case "Sid": flag = deleteStudentBySid((String) object); break; case "id": case "Id": flag = deleteStudentById((int) object); break; case "name": case "Name": flag = deleteStudentByName((String) object); break; case "age": case "Age": flag = deleteStudentByAge((Integer) object); break; case "sex": case "Sex": flag = deleteStudentBySex((String) object); break; } return flag; } /** * Delete students according to student Sid * * @param sid * @return */ public boolean deleteStudentBySid(String sid) { boolean result; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); result = StudentService.deleteStudentBySid(sid) > 0 ? true : false; sqlSession.close(); return result; } /** * Delete students according to student number * * @param id Student Id number * @return True will be returned if the deletion is successful, otherwise false will be returned */ public boolean deleteStudentById(int id) { boolean result; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); result = StudentService.deleteStudentById(id) > 0 ? true : false; sqlSession.close(); return result; } /** * Delete students by name * * @param name Student name * @return True will be returned if the deletion is successful, otherwise false will be returned */ public boolean deleteStudentByName(String name) { boolean result; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); result = StudentService.deleteStudentByName(name) > 0 ? true : false; sqlSession.close(); return result; } /** * Delete students by age * * @param age Student's age * @return True will be returned if the deletion is successful, otherwise false will be returned */ public boolean deleteStudentByAge(int age) { boolean result; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); result = StudentService.deleteStudentByAge(age) > 0 ? true : false; sqlSession.close(); return result; } /** * Delete students by gender * * @param sex Gender of students * @return True will be returned if the deletion is successful, otherwise false will be returned */ public boolean deleteStudentBySex(String sex) { boolean result; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); result = StudentService.deleteStudentBySex(sex) > 0 ? true : false; sqlSession.close(); return result; } /** * Update student information according to the provided student objects * * @param student Object of students * @return If the update is successful, it returns true. If not, it returns false */ public boolean updateStudent(Student student) { boolean result = false; SqlSession sqlSession = MyBatisUtil.getSqlSession(); StudentService StudentService = sqlSession.getMapper(StudentService.class); result = StudentService.updateStudent(student) > 0 ? true : false; sqlSession.close(); return result; } }
(2) Test code