mybatis-config.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"> <configuration> <!--引入外部properties配置文件--> <properties resource="confdbconfig.properties"></properties> <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> <mappers> <mapper resource="studentMapper.xml"/> </mappers> </configuration>
dbconfig.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db_person username=root password=1234
相应的StudentMapper接口
package dao; import bean.Student; public interface StudentMapper { public Student getById(Integer id); }
以及StudentMapper.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="test.dao.StudentMapper"> <!--public Student getById(Integer id);--> <select id="getById" resultType="bean.Student"> SELECT * FROM student WHERE id=#{id} </select> </mapper>