标签:depend 需要 stream images false stat led app java接口
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.shyrolk</groupId> <artifactId>firstMybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>firstMybatis</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> </dependencies> </project>
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC" /> <property name="username" value="root" /> <property name="password" value="" /> </dataSource> </environment> </environments> </configuration>
package com.shyroke.entity; public class User { private int id; private String userName; private String passWord; public int getId() { return id; } public void setId(int 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; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", passWord=" + passWord + "]"; } }
package com.shyroke.mapper; import org.apache.ibatis.annotations.Param; import com.shyroke.entity.User; public interface UserMapper { public User findUserById(@Param("id")int id); }
<?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.shyroke.mapper.UserMapper"> <!-- 根据 id 查询 user 表中的数据 id:唯一标识符,此文件中的id值不能重复 resultType:返回值类型,一条数据库记录也就对应实体类的一个对象 parameterType:参数类型,也就是查询条件的类型 --> <select id="findUserById" resultType="com.shyroke.entity.User" parameterType="int"> <!-- 这里和普通的sql 查询语句差不多,后面的 #{id}表示占位符,里面不一定要写id,写啥都可以,但是不要空着 --> select * from user where id = #{id} </select> </mapper>
UserMapper.xml和UserMapper.java两个文件的文件名要一致且要处于同一个目录。
<mappers> <mapper class="com.shyroke.mapper.UserMapper"/> </mappers>
package com.shyrolk.firstMybatis; import java.awt.image.ImageProducer; import java.io.InputStream; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.shyroke.entity.User; import com.shyroke.mapper.UserMapper; /** * Hello world! * */ public class App { public static void main( String[] args ) { InputStream inputStream=App.class.getClassLoader().getResourceAsStream("resource/mybatis-config.xml"); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession=sqlSessionFactory.openSession(); UserMapper userMapper=sqlSession.getMapper(UserMapper.class); User user= userMapper.findUserById(1); System.out.println(user); sqlSession.close(); } }
结果:
标签:depend 需要 stream images false stat led app java接口
原文地址:http://www.cnblogs.com/shyroke/p/7600994.html