标签:config 语句 nts 连接 port 配置文件 environ import 文件
创建一个maven工程,在pom文件中加载依赖
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> </dependencies>
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userName` varchar(50) DEFAULT NULL, `userAge` int(11) DEFAULT NULL, `userAddress` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; Insert INTO `user` VALUES (‘1‘, ‘summer‘, ‘100‘, ‘shanghai,pudong‘);
<?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>
<!--定义别名--> <typeAliases> <typeAlias alias="User" type="com.yihaomen.mybatis.model.User"/> </typeAliases> <!--定义数据库信息,默认使用development数据库构建环境--> <environments default="development"> <environment id="development">
<!--采用JDBC事务管理--> <transactionManager type="JDBC"/>
<!--配置数据库连接信息--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis-learn" /> <property name="username" value="root"/> <property name="password" value="tiger"/> </dataSource> </environment> </environments> <!--定义映射器--> <mappers> <mapper resource="com/yihaomen/mybatis/model/User.xml"/> </mappers> </configuration>
public class User { private int id; private String userName; private String userAge; private String userAddress;
setters & getters }
<?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.yihaomen.mybatis.models.UserMapper"> <select id="selectUserByID" parameterType="int" resultType="User"> select * from `user` where id = #{id} </select> </mapper>
import com.yihaomen.mybatis.model.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 java.io.IOException; import java.io.Reader; public class Test { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; static { try { reader = Resources.getResourceAsReader("configuration.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } } public static SqlSessionFactory getSession() { return sqlSessionFactory; } public static void main(String[] args) { SqlSession session = sqlSessionFactory.openSession(); try { User user = (User) session.selectOne("com.yihaomen.mybatis.models.UserMapper.selectUserByID", 1); System.out.println(user.getUserAddress()); } finally { session.close(); } } }
https://gitee.com/huayicompany/springmvc-mybatis
标签:config 语句 nts 连接 port 配置文件 environ import 文件
原文地址:http://www.cnblogs.com/happyflyingpig/p/7653224.html