Preparatory work:
1. Create a database
2. Create a new user table. The table fields include id, username, birthday, sex and address
3. Download and unzip the maven installation package
New maven project:
1,File-new-Project
2. Select maven and click next
3. GroupId generally fills in the company domain name and writes it backwards. ArtifactId writes the project name. Version is the project version number. Click next
4. Click next, and then finish directly
5. After the project is created, select allow automatic import in the lower right corner
6. Create two new configuration files under src – main – resources (JDBC config. Properties & & log4j. Properties)
① contents of JDBC config.properties:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306 / database name jdbc.username=Database user name jdbc.password=Database password
② log4j.properties:
# Set root category priority to INFO and its only appender to CONSOLE. #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal log4j.rootCategory=debug, CONSOLE, LOGFILE # Set the enterprise logger category to FATAL and its only appender to CONSOLE. log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE # CONSOLE is set to be a ConsoleAppender using a PatternLayout. log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n # LOGFILE is set to be a File appender using a PatternLayout. log4j.appender.LOGFILE=org.apache.log4j.FileAppender log4j.appender.LOGFILE.File=d:\axis.log**(Note here that the computer needs to have d Otherwise, it will fail to create the log file d Change the drive to the drive letter owned by your computer)** log4j.appender.LOGFILE.Append=true log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
7. Import related dependency packages in pom.xml
<dependencies> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> </dependencies>
8. Create a new User entity class in the entity layer, and provide the corresponding get, set and other methods:
package com.xmcc.entity; import lombok.*; import java.io.Serializable; import java.util.Date; /** * @author zxl * @date 2019-03-06 16:44 */ @Setter @Getter @AllArgsConstructor @NoArgsConstructor @ToString public class User implements Serializable { private int id; private String username; private Date birthday; private String sex; private String address; public User(int id, String sex) { this.id = id; this.sex = sex; } public User(int id) { this.id = id; } }
9. Create a new interface UserDao.java in dao layer
Provide a simple method of adding, deleting, modifying and checking correspondence
package com.xmcc.dao; import com.xmcc.entity.User; import java.util.List; public interface UserDao { List<User> findAll(); void update(User user); void insert(User user); void delete(User user); }
10. Create a UserDao.xml file with the same name under the same directory structure under resources
11. Import constraints in SqlMapConfig.xml and configure them
<?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> <!--Load profile--> <properties resource="jdbcConfig.properties"></properties> <!--To configure mybatis Environmental Science--> <environments default="development"> <environment id="development"> <!-- transaction management --> <transactionManager type="JDBC"></transactionManager> <!-- data source --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </dataSource> </environment> </environments> <!--Import mapping profile--> <mappers> <mapper resource="com/xmcc/dao/UserDao.xml"></mapper> </mappers> </configuration>
12. Import constraints in UserDao.xml and implement corresponding methods of UserDao interface
<?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 Interface full pathname--> <mapper namespace="com.xmcc.dao.UserDao"> <select id="findAll" resultType="com.xmcc.entity.User"> select * from user; </select> <update id="update" parameterType="com.xmcc.entity.User"> update user set sex=#{sex} where id=#{id}; </update> <insert id="insert" parameterType="com.xmcc.entity.User"> insert into user values(#{id},#{username},#{birthday},#{sex},#{address}); </insert> <delete id="delete" parameterType="com.xmcc.entity.User"> delete from user where id=#{id}; </delete> </mapper>
13. Write test class
Test the findAll method:
//1. Read configuration file InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2. Create SqlSessionFactory construction object SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); //3. Create SqlSessionFactory SqlSessionFactory factory = builder.build(in); //4. Open sqlSession (operate database object) SqlSession sqlSession = factory.openSession(); //5. Create dao interface proxy object with sqlSession UserDao dao = sqlSession.getMapper(UserDao.class); List<User> all = dao.findAll(); System.out.println(all); //6. Release resources sqlSession.close();
Similar to other methods, adding, modifying, and deleting a transaction need to be submitted actively after the method is called
//Submit event
sqlSession.commit();
It's not easy to write. Please support more. If you have any questions, please contact email 1776787744@qq.com