首先创一个maven项目,这里就不多说了。下图是作者的目录结构
配置pom.xml(复制直接使用即可)
<!--统一管理所有jar包版本号--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.2.5.RELEASE</spring.version> <aopalliance.version>1.0</aopalliance.version> <aspectj.weaver>1.9.2</aspectj.weaver> <jstl.version>1.2</jstl.version> <jsp-api.version>2.0</jsp-api.version> <servlet-api.version>3.1.0</servlet-api.version> <mybatis.version>3.4.6</mybatis.version> <mybatis.spring.version>1.3.2</mybatis.spring.version> <mysql.version>5.1.3</mysql.version> <c3p0.version>0.9.5.2</c3p0.version> <log4j.version>1.7.2</log4j.version> <commons-logging.version>1.2</commons-logging.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <commons-io.version>2.4</commons-io.version> <pagehelper.version>5.1.2</pagehelper.version> <jsqlparser.version>0.9.1</jsqlparser.version> </properties> <dependencies> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!--spring mvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!-- AOP联盟的API包 --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>${aopalliance.version}</version> </dependency> <!-- AspectJ Weaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.weaver}</version> </dependency> <!-- Jackson Json处理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.9</version> </dependency> <!-- Excel工具包 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <!-- 富文本编辑器 --> <dependency> <groupId>com.gitee.qdbp.thirdparty</groupId> <artifactId>ueditor</artifactId> <version>1.4.3.3</version> </dependency> <!-- JSP相关 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!--mybatis spring 插件 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- mysql连接 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- c3p0数据库连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>${c3p0.version}</version> </dependency> <!--阿里连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.22</version> </dependency> <!-- log4j日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${log4j.version}</version> </dependency> <!-- <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <version>2.9.1</version> </dependency>--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>${commons-logging.version}</version> </dependency> <!-- 文件上传 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <!-- 分页 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>${jsqlparser.version}</version> </dependency> <!--配置redis--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.10.2</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.3.0.RELEASE</version> </dependency> <!--单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.10.2</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.3.0.RELEASE</version> </dependency> <!--配置代码生成器依赖--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> <build> <!--配置内部tomcat插件--> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/</path> <port>9999</port> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin> <!--配置代码生成器插件--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!--此处驱动版本和上面同步--> <version>5.1.3</version> </dependency> </dependencies> <configuration> <!--配置文件的路径--> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> </configuration> </plugin> </plugins> <!--配置资源打包方式 解决maven 找不到mapper映射文件:放在resources里面不用配置--> <resources> <resource> <directory>src/main/java</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>
再就是log4j.properties文件和jdbc.properties文件
log4j.properties
### direct log message to stdout ### log4j.appender.stdout.Target = System.out log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n #当前位置的包名 只要包含 mapper log4j.logger.com.lyh.ssm=TRACE log4j.rootLogger=INFO, stdout
jdbc.properties
driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/hisystem?useUnicode=true&characterEncoding=utf-8 username=root password=123456
mybatis.xml(复制使用直接改文件路径即可)
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--读取配置文件 --> <properties resource="jdbc.properties"></properties> <!--配置别名 --> <typeAliases> <package name="com.lyh.ssm.entity"/> </typeAliases> <!--环境配置 --> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${driverClass}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!--注册映射文件--> <mappers> <!--<package name="com.lyh.ssm.mapper"/>--> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
到这里环境就已经搭好了,接下来到UserTest,@Before下先创建工厂,@After中关闭资源
import com.lyh.ssm.entity.Idcard;
import com.lyh.ssm.mapper.UserMapper;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mybatis.spring.SqlSessionFactoryBean;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class UserTest {
private SqlSession session;
private InputStream is;
@Before
public void before() throws IOException {
//读取核心配置文件
is= Resources.getResourceAsStream("mybatis.xml");
//拿到工厂构建类
SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
//创建工厂
SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is);
//创建session 对象
session=sqlSessionFactory.openSession();
}
@After
public void after(){
session.commit();
session.close();
}
//接下来在这下面写一些增删改查来进行SQL练习.
@Test public void test(){ //获取具体的mapper对象 UserMapper mapper=session.getMapper(UserMapper.class); //执行业务逻辑,调接口 //全查询 System.out.println("======================find======================"); List<Idcard> list=mapper.find(); list.forEach(u -> System.out.println(u.getName())); //根据id查询 System.out.println("======================findById=================="); Idcard idcard=mapper.findById(2); System.out.println(idcard); } @Test public void add(){ //添加 System.out.println("======================add=================="); Idcard idcard=new Idcard(); UserMapper mapper=session.getMapper(UserMapper.class); idcard.setId(7); // idcard.setId(6); idcard.setAddress("郑州"); idcard.setName("头子"); int add=mapper.add(idcard); System.out.println("add"+add); System.out.println(idcard); } @Test public void upp(){ System.out.println("======================upp=================="); Idcard idcard=new Idcard(); UserMapper mapper=session.getMapper(UserMapper.class); idcard.setName("迪子"); idcard.setAddress("上海"); idcard.setId(6); int upp=mapper.upp(idcard); System.out.println("upp:"+upp); System.out.println(idcard); } @Test public void del(){ System.out.println("======================del=================="); Idcard idcard=new Idcard(); UserMapper mapper=session.getMapper(UserMapper.class); int del=mapper.del(5); System.out.println("del:"+del); }
}
实体类Idcard
package com.lyh.ssm.entity; import lombok.Data; @Data public class Idcard { private int id; private String name; private String sex; private String nationality; private String id_card; private String address; private String birthday; }
UserMapper接口
import java.util.List; public interface UserMapper { List<Idcard> find(); Idcard findById(int i); int add(Idcard idcard); int upp(Idcard idcard); int del(int i); }
UserMapper.xml
<?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.lyh.ssm.mapper.UserMapper">
<!--将查询结果的字段和实体中的字段进行映射关联-->
<resultMap id="IdcardMap" type="Idcard">
<id property="id" column="id"></id>
<result property="name" column="name"></result>
</resultMap>
<select id="find" resultMap="IdcardMap" parameterType="Idcard">
select * from Idcard
</select>
<select id="findById" resultMap="IdcardMap" parameterType="Idcard">
select * from Idcard where id=#{id}
</select>
<insert id="add" parameterType="Idcard">
insert into idcard
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id!=null and id!=''">id,</if>
<if test="name!=null and name!=''">name,</if>
<if test="sex!=null and sex!=''">sex,</if>
<if test="nationality!=null and nationality!=''">nationality,</if>
<if test="id_card!=null and id_card!=''">id_card,</if>
<if test="address!=null and address!=''">address,</if>
<if test="birthday!=null and birthday!=''">birthday,</if>
</trim>
<trim prefix="values(" suffix=")" suffixOverrides=",">
<if test="id!=null and id!=''">#{id},</if>
<if test="name!=null and name!=''">#{name},</if>
<if test="sex!=null and sex!=''">#{sex},</if>
<if test="nationality!=null and nationality!=''">#{nationality},</if>
<if test="id_card!=null and id_card!=''">#{id_card},</if>
<if test="address!=null and address!=''">#{address},</if>
<if test="birthday!=null and birthday!=''">#{birthday},</if>
</trim>
</insert>
<update id="upp" parameterType="Idcard">
update idcard
<set>
<if test="name!=null and name!=''">name=#{name},</if>
<if test="sex!=null and sex!=''">sex=#{sex},</if>
<if test="nationality!=null and nationality!=''">nationality=#{nationality},</if>
<if test="id_card!=null and id_card!=''">id_card=#{id_card},</if>
<if test="address!=null and address!=''">address=#{address},</if>
<if test="birthday!=null and birthday!=''">birthday=#{birthday},</if>
</set>
where id=#{id}
</update>
<delete id="del" parameterType="Idcard">
delete from idcard where id=#{id}
</delete>
</mapper>
实现的效果
到这里也就结束了谢谢观看!☺