哈尔滨网站建设价位,沅江网站设计公司,石家庄的网站建设公司哪家好,网站论坛推广方案项目实现的功能
查询所有用户信息 通过Id查询用户信息 添加用户#xff08;回显主键#xff09; 修改用户信息 删除用户信息 通过用户名字模糊查询
一、引入依赖和工程结构 ?xml version1.0 encodingUTF-8?
project xmlnshttp…项目实现的功能
查询所有用户信息 通过Id查询用户信息 添加用户回显主键 修改用户信息 删除用户信息 通过用户名字模糊查询
一、引入依赖和工程结构 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.william/groupIdartifactIdMybatisMapper/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target/propertiesdependenciesdependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/version/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.4.5/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.47/version/dependency/dependencies/project二、创建实体类
User
package com.william.domain;import java.util.Date;/*** author lijunxuan* date Created in 2019/7/9 19:28* description * version: 1.0*/
public class User {private Integer id;private String username;private String password;private String sex;private Date birthday;Overridepublic String toString() {return User{ id id , username username \ , password password \ , sex sex \ , birthday birthday };}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday birthday;}public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public String getSex() {return sex;}public void setSex(String sex) {this.sex sex;}}
三、映射文件
1.UserMapper.xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.william.dao.UserMapper!--查询所有用户信息--select idfindAll resultTypecom.william.domain.Userselect * from user/select!--通过ID查询用户信息--select idfindById parameterTypejava.lang.Integer resultTypecom.william.domain.Userselect * from user where id#{id}/select!--模糊查询--select idfindByUsername parameterTypejava.lang.String resultTypecom.william.domain.Userselect * from user where username like %#{value}%/select!--增加用户信息 主键回显--insert idinsert parameterTypecom.william.domain.User
-- selectKey: 查询主键值
-- resultType 主键类型
-- keyColumn: 指定主键的列名
-- keyProperty: 主键对应的属性名
-- order:可选值after before
-- AFTER: 在执行sql语句之后执行查找主键操作
-- BEFORE: 在执行sql语句之前执行查找主键操作selectKey resultTypejava.lang.Integer keyColumnid keyPropertyid orderAFTER select last_insert_id()/selectKeyinsert into user values (null ,#{username},#{password},#{sex},#{birthday})/insert!--更新用户信息--update idupdate parameterTypecom.william.domain.Userupdate user set username#{username},password#{password},sex#{sex},birthday#{birthday} where id#{id}/update!--删除用户信息--delete iddelete parameterTypejava.lang.Integerdelete from user where id #{id}/delete
/mapper四、引入log4j
log4j.properties
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategoryINFO, CONSOLE debug info warn error fatal
log4j.rootCategorydebug, CONSOLE, LOGFILE, info# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterpriseFATAL, CONSOLE# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLEorg.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layoutorg.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.LOGFILEorg.apache.log4j.FileAppender
log4j.appender.LOGFILE.Filed:\axis.log
log4j.appender.LOGFILE.Appendtrue
log4j.appender.LOGFILE.layoutorg.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
五、测试类
TestCrud
package com.william;import com.william.dao.UserMapper;
import com.william.domain.User;
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.Test;import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;/*** author lijunxuan* date Created in 2019/7/12 10:16* description * version: 1.0*/
public class TestCrud {/*** 查询所有用户信息* throws IOException*/Testpublic void findAll() throws IOException {InputStream inputStream Resources.getResourceAsStream(Mybatis-configuration.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession sqlSessionFactory.openSession();//获取某接口的动态代理对象获取某接口的一个实现类UserMapper userMapper sqlSession.getMapper(UserMapper.class);ListUser userList userMapper.findAll();for (User user : userList) {System.out.println(user);}sqlSession.close();}/****通过Id查询用户信息* throws IOException*/Testpublic void findById() throws IOException {InputStream inputStream Resources.getResourceAsStream(Mybatis-configuration.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession sqlSessionFactory.openSession();//获取某接口的动态代理对象获取某接口的一个实现类UserMapper userMapper sqlSession.getMapper(UserMapper.class);User findByIdUser userMapper.findById(43);System.out.println(findByIdUser);sqlSession.close();}/*** 模糊查询* 通过用户名模糊查询* throws IOException*/Testpublic void findByUsername() throws IOException {InputStream inputStream Resources.getResourceAsStream(Mybatis-configuration.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession sqlSessionFactory.openSession();//获取某接口的动态代理对象获取某接口的一个实现类UserMapper userMapper sqlSession.getMapper(UserMapper.class);ListUser findByIdUsernameList userMapper.findByUsername(a);for (User user : findByIdUsernameList) {System.out.println(user);}sqlSession.close();}/*** 增加用户信息* throws IOException*/Testpublic void insert() throws IOException {InputStream inputStream Resources.getResourceAsStream(Mybatis-configuration.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession sqlSessionFactory.openSession();//获取某接口的动态代理对象获取某接口的一个实现类UserMapper userMapper sqlSession.getMapper(UserMapper.class);User user new User();user.setUsername(today);user.setPassword(hello);user.setSex(女);userMapper. insert(user);//事务提交sqlSession.commit();System.out.println(添加后用户的id为user.getId());sqlSession.close();}/*** 更新用户信息* throws IOException*/Testpublic void update() throws IOException {InputStream inputStream Resources.getResourceAsStream(Mybatis-configuration.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession sqlSessionFactory.openSession();//获取某接口的动态代理对象获取某接口的一个实现类UserMapper userMapper sqlSession.getMapper(UserMapper.class);User user new User();user.setId(45);user.setUsername(today);user.setPassword(hello);user.setSex(女);userMapper. update(user);//事务提交sqlSession.commit();sqlSession.close();}/*** 通过id删除用户信息* throws IOException*/Testpublic void delete() throws IOException {InputStream inputStream Resources.getResourceAsStream(Mybatis-configuration.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession sqlSessionFactory.openSession();//获取某接口的动态代理对象获取某接口的一个实现类UserMapper userMapper sqlSession.getMapper(UserMapper.class);userMapper. delete(50);//事务提交sqlSession.commit();sqlSession.close();}
}
【重点】mybatis动态代理模式开发的要求
1.dao接口和映射文件必须路径相同文件名相同(dao接口和映射文件必须在同一个文件夹中文件名必须相同)
如图所示
2.namespace必须是 接口的全限定类名 3.映射文件中的id值必须是接口的方法名 4.参数类型和返回值类也必须是匹配的