第一个Mybatis程序
maven依赖
<!--导入依赖--> <dependencies> <!--导入mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</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>4.12</version> </dependency> </dependencies>
编写一个mybatis.xml配置文件
<?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"> <!--configguration核心配置文件--> <configuration> <!--导入外部配置文件--> <properties resource="db.properties"/> <!--导入日志功能--> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!--起別名--> <typeAliases> <typeAlias type="com.rzk.pojo.User" alias="User"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!--每一个Mapper.xml 都需要在Mybatis核心配置文件中注册--> <mappers> <mapper resource="com/rzk/mapper/UserMapper.xml"/> </mappers> </configuration>
db.properties文件
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/数据库名?userSSL=true&useUnicode=true&characterEncoding=UTF8 username=root password=123456
写一个工具类
public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ try { String resources="mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resources); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSessionFactory() { return sqlSessionFactory.openSession(); } }
编写一个实体类
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(20) NOT NULL, `name` varchar(30) DEFAULT NULL, `pwd` varchar(30) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES (‘1‘, ‘李倩‘, ‘123123‘); INSERT INTO `user` VALUES (‘2‘, ‘张三‘, ‘123456‘); INSERT INTO `user` VALUES (‘3‘, ‘李四‘, ‘123456‘); INSERT INTO `user` VALUES (‘4‘, ‘赵柳‘, ‘1234556‘); INSERT INTO `user` VALUES (‘6‘, ‘libai‘, ‘123456‘); INSERT INTO `user` VALUES (‘7‘, ‘lisi‘, ‘123123‘); INSERT INTO `user` VALUES (‘8‘, ‘聊吧‘, ‘123123‘);
写一个UserDao接口
public interface UserMapper { //查询用户 List<User> getUserList(); }
在resources编写对应的mapper文件
<?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绑定一个对应的Dao/Mapper接口--> <mapper namespace="com.rzk.mapper.UserMapper"> <select id="getUserList" resultType="com.rzk.pojo.User"> select * from user </select> </mapper>
日志功能
log4j.rootLogger=DEBUG,console,file #控制台输出的相关设置 log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.Threshold=DEBUG log4j.appender.console.layout = org.apache.log4j.PatternLayout #log4j.appender.console.layout.ConversionPattern=[%c]-%m%n log4j.appender.console.layout.ConversionPattern=%5p [%t] - %m%n #文件输出的相关设置 log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/rzk.log log4j.appender.file.MaxFileSize=10mb log4j.appender.file.Threshold=DEBUG log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n #日志输出级别 log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
测试
@Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserList(); for (User user : userList) { System.out.println(user); } }
相关推荐
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
dongxurr 2020-08-08
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
Dullonjiang 2020-07-30
jimgreatly 2020-07-27
liqiancao 2020-07-26
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26
helloxusir 2020-07-25
牧场SZShepherd 2020-07-20