spring整合mybatis基于注解(去xml配置)
【摘要】
舍弃繁琐的xml装配,我们用简单方式在Java类中装配
目录结构配置springConfigl类Mapper接口Service类来个测试类最后把输出贴出来看看,就不截图了,Linux系统截图不方便
目录结构
配置springConfigl类
软加载方式把db.properties文件内容加载到JdbcInfo的属性上
db.peoper...
目录结构
配置springConfigl类
软加载方式把db.properties文件内容加载到JdbcInfo的属性上
db.peoperties内容
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mxd01?characterEncoding=utf8
mysql.username=root
mysql.password=**
@Component
@PropertySource("classpath:db.properties")
public class JdbcInfo { @Value("${mysql.driver}") private String driver; @Value("${mysql.url}") private String url; @Value("${mysql.username}") private String username;
@Value("${mysql.password}") private String password; public String getDriver() { return driver; } public String getUrl() { return url; } public String getUsername() { return username; } public String getPassword() { return password; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
@Configuration
@ComponentScan({"config","dao","entity","service"})
@MapperScan("dao")
public class SpringConfig { @Resource private JdbcInfo jdbcInfo; @Bean public DataSource getDataSource() {
//在这里我们用当前最快的HikariCP连接池来生成dataSource HikariConfig hikariConfig = new HikariConfig(); hikariConfig.setDriverClassName(jdbcInfo.getDriver()); hikariConfig.setPassword(jdbcInfo.getPassword()); hikariConfig.setUsername(jdbcInfo.getUsername()); hikariConfig.setJdbcUrl(jdbcInfo.getUrl()); HikariDataSource dataSource = new HikariDataSource(hikariConfig); return dataSource; } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{ //操作mybatis要先生成一个SqlSessionFactoryBean类 SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean.getObject(); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
Mapper接口
@Repository
@Mapper
public interface PersonMapper1 { @Select("SELECT * FROM person WHERE id= #{personId}") //按照学号查学生 Person getPerson(@Param("personId") int personId); @Select("select * from person") //输出所有学生到List中 List<Person> queryAll();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
Service类
@Service
public class PersonService1 { @Autowired
private PersonMapper1 personMapper1; public void queryId(int id){ System.out.println(personMapper1.getPerson(id)); } public void queryAll(){ List<Person> people = personMapper1.queryAll(); for (Person person : people) { System.out.println(people); } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
来个测试类
public class main { public static void main(String[] args) { //加载注解的spring配置类 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
//根据.class文件取出Bean,再执行相关方法 PersonService1 bean = context.getBean(PersonService1.class); bean.queryId(1); bean.queryAll(); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
最后把输出贴出来看看,就不截图了,Linux系统截图不方便
Person{id=1, name=‘mxd’, age=2}
[Person{id=1, name=‘mxd’, age=2}, Person{id=2, name=‘mmm’, age=19}, Person{id=100, name=‘mmmmm’, age=10}]
文章来源: blog.csdn.net,作者:East Horse,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_44613138/article/details/105664655
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)