IDEA使⽤Spring、Mybatis实现简单的数据库查询。
⼀、创建⼀个mavan项⽬
⼆、创建两个module,dao层和entity层
三、在根层添加spring-context,spring-core,spring-beans,spring-jdbc,mybatis,mysql等dao层依赖    <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId&hange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
</project>
四、在dao层创建⼀个StudentMapper接⼝
五、在接⼝内添加⼀个findAllStudent查询学⽣⽅法
六、在dao层resources⽬录创建⼀个mapper⽬录⽤于存放l映射⽂件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nf.mapper.StudentMapper">
<resultMap id="resultStudent" type="ity.Student">
<id property="id" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="dob" column="dbd"/>
</resultMap>
<select id="findAllStudent" resultMap="resultStudent">
select * from students
</select>
</mapper>
七、在resources⽬录下创建l Spring容器配置⽂件
applicationContext代码如下:myDataSource连接池中记录着连接信息,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd">
<bean id="myDataSource" class="hange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="sql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/batis?serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true"></property>        <property name="user" value="root"></property>
<property name="password" value="1101"></property>
</bean>
<bean id="mySqlSessionFactory" class="batis.spring.SqlSessionFactoryBean">
     //引⽤了myDataSource连接池
数据库简单吗<property name="dataSource" ref="myDataSource"></property>
     //在mapper⽬录下到l映射⽂件
<property name="mapperLocations" value="classpath*:mapper/*.xml"></property>
</bean>
</beans>
⼋、在entity层创建Student实体类,并使⽤快捷⽅式写出get()set()⽅法
九、在dao层再创建⼀个test包写⼀个Test类,在Test类中写main()⽅法开始测试
测试代码如下:
package st;
import com.nf.mapper.StudentMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import t.ApplicationContext;
import t.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String args[]){
     //实例化⼀个ClassPathXmlApplicationContext通过⽂件名查⽅式到
ApplicationContext ac = new ClassPathXmlApplicationContext("l");
     //通过getBean⽅法到applicationContext中的mySqlSessionFactory
SqlSessionFactory factory = (SqlSessionFactory) ac.getBean("mySqlSessionFactory");
SqlSession sqlSession = factory.openSession();
StudentMapper studao = Mapper(StudentMapper.class);
     //通过findAllStudent()⽅法查询数据库打印出数据的长度
System.out.println(studao.findAllStudent().size());
}
}
⼗、测试结果成功,为七条
数据库中的数据为七条,验证成功

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。